home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / packages / vc.el < prev    next >
Encoding:
Text File  |  1995-09-01  |  91.4 KB  |  2,488 lines

  1. ;;; vc.el --- drive a version-control system from within Emacs
  2.  
  3. ;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
  4. ;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
  5.  
  6. ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
  7. ;; Maintainer: ttn@netcom.com
  8. ;; Version: 5.6
  9.  
  10. ;; This file is part of XEmacs.
  11.  
  12. ;; XEmacs is free software; you can redistribute it and/or modify it
  13. ;; under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; XEmacs is distributed in the hope that it will be useful, but
  18. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20. ;; General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  24. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; This mode is fully documented in the Emacs user's manual.
  29. ;;
  30. ;; This was designed and implemented by Eric Raymond <esr@snark.thyrsus.com>.
  31. ;; Paul Eggert <eggert@twinsun.com>, Sebastian Kremer <sk@thp.uni-koeln.de>,
  32. ;; and Richard Stallman contributed valuable criticism, support, and testing.
  33. ;; CVS support was added by Per Cederqvist <ceder@lysator.liu.se>
  34. ;; in Jan-Feb 1994.
  35. ;;
  36. ;; XEmacs fixes, CVS fixes, and general improvements
  37. ;; by Jonathan Stigelman <Stig@hackvan.com>
  38. ;;
  39. ;; Supported version-control systems presently include SCCS, RCS, and CVS.
  40. ;; The RCS lock-stealing code doesn't work right unless you use RCS 5.6.2
  41. ;; or newer.  Currently (January 1994) that is only a beta test release.
  42. ;; Even initial checkins will fail if your RCS version is so old that ci
  43. ;; doesn't understand -t-; this has been known to happen to people running
  44. ;; NExTSTEP 3.0. 
  45. ;;
  46. ;; The RCS code assumes strict locking.  You can support the RCS -x option
  47. ;; by adding pairs to the vc-master-templates list.
  48. ;;
  49. ;; Proper function of the SCCS diff commands requires the shellscript vcdiff
  50. ;; to be installed somewhere on Emacs's path for executables.
  51. ;;
  52. ;; If your site uses the ChangeLog convention supported by Emacs, the
  53. ;; function vc-comment-to-change-log should prove a useful checkin hook.
  54. ;;
  55. ;; This code depends on call-process passing back the subprocess exit
  56. ;; status.  Thus, you need Emacs 18.58 or later to run it.  For the
  57. ;; vc-directory command to work properly as documented, you need 19.
  58. ;; You also need Emacs 19's ring.el.
  59. ;;
  60. ;; The vc code maintains some internal state in order to reduce expensive
  61. ;; version-control operations to a minimum.  Some names are only computed
  62. ;; once.  If you perform version control operations with RCS/SCCS/CVS while
  63. ;; vc's back is turned, or move/rename master files while vc is running,
  64. ;; vc may get seriously confused.  Don't do these things!
  65. ;;
  66. ;; Developer's notes on some concurrency issues are included at the end of
  67. ;; the file.
  68.  
  69. ;;; Code:
  70.  
  71. (require 'vc-hooks)
  72. (require 'ring)
  73. (eval-when-compile (require 'dired))    ; for dired-map-over-marks macro
  74.  
  75. (if (not (assoc 'vc-parent-buffer minor-mode-alist))
  76.     (setq minor-mode-alist
  77.       (cons '(vc-parent-buffer vc-parent-buffer-name)
  78.         minor-mode-alist)))
  79.  
  80. ;; General customization
  81.  
  82. (defvar vc-default-back-end nil
  83.   "*Back-end actually used by this interface; may be SCCS or RCS.
  84. The value is only computed when needed to avoid an expensive search.")
  85. (defvar vc-suppress-confirm nil
  86.   "*If non-nil, treat user as expert; suppress yes-no prompts on some things.")
  87. (defvar vc-keep-workfiles t
  88.   "*If non-nil, don't delete working files after registering changes.
  89. If the back-end is CVS, workfiles are always kept, regardless of the
  90. value of this flag.")
  91. (defvar vc-initial-comment nil
  92.   "*Prompt for initial comment when a file is registered.")
  93. (defvar vc-command-messages nil
  94.   "*Display run messages from back-end commands.")
  95. (defvar vc-mistrust-permissions 'file-symlink-p
  96.   "*Don't assume that permissions and ownership track version-control status.")
  97. (defvar vc-checkin-switches nil
  98.   "*Extra switches passed to the checkin program by \\[vc-checkin].")
  99. (defvar vc-checkout-switches nil
  100.   "*Extra switches passed to the checkout program by \\[vc-checkout].")
  101. (defvar vc-path
  102.   (if (file-exists-p "/usr/sccs")
  103.       '("/usr/sccs") nil)
  104.   "*List of extra directories to search for version control commands.")
  105. (defvar vc-directory-exclusion-list '("SCCS" "RCS")
  106.   "*Directory names ignored by functions that recursively walk file trees.")
  107.  
  108. (defconst vc-maximum-comment-ring-size 32
  109.   "Maximum number of saved comments in the comment ring.")
  110.  
  111. ;;; XEmacs - This is dumped into loaddefs.el already.
  112. ;; (defvar diff-switches "-c"
  113. ;;   "*A string or list of strings specifying switches to be be passed to diff.")
  114.  
  115. ;;;###autoload
  116. (defvar vc-checkin-hook nil
  117.   "*List of functions called after a checkin is done.  See `run-hooks'.")
  118.  
  119. (defvar vc-make-buffer-writable-hook nil
  120.   "*List of functions called when a buffer is made writable.  See `run-hooks.'
  121. This hook is only used when the version control system is CVS.  It
  122. might be useful for sites who uses locking with CVS, or who uses link
  123. farms to gold trees.")
  124.  
  125. ;; Header-insertion hair
  126.  
  127. (defvar vc-header-alist
  128.   '((SCCS "\%W\%") (RCS "\$Id\$") (CVS "\$Id\$"))
  129.   "*Header keywords to be inserted when `vc-insert-headers' is executed.")
  130. (defvar vc-static-header-alist
  131.   '(("\\.c$" .
  132.      "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
  133.   "*Associate static header string templates with file types.  A \%s in the
  134. template is replaced with the first string associated with the file's
  135. version-control type in `vc-header-alist'.")
  136.  
  137. (defvar vc-comment-alist
  138.   '((nroff-mode ".\\\"" ""))
  139.   "*Special comment delimiters to be used in generating vc headers only.
  140. Add an entry in this list if you need to override the normal comment-start
  141. and comment-end variables.  This will only be necessary if the mode language
  142. is sensitive to blank lines.")
  143.  
  144. ;; Default is to be extra careful for super-user.
  145. (defvar vc-checkout-carefully (= (user-uid) 0) ; #### - this prevents preloading!
  146.   "*Non-nil means be extra-careful in checkout.
  147. Verify that the file really is not locked
  148. and that its contents match what the master file says.")
  149.  
  150. ;; Variables the user doesn't need to know about.
  151. (defvar vc-log-entry-mode nil)
  152. (defvar vc-log-operation nil)
  153. (defvar vc-log-after-operation-hook nil)
  154. (defvar vc-checkout-writable-buffer-hook 'vc-checkout-writable-buffer)
  155. ;; In a log entry buffer, this is a local variable
  156. ;; that points to the buffer for which it was made
  157. ;; (either a file, or a VC dired buffer).
  158. (defvar vc-parent-buffer nil)
  159. (defvar vc-parent-buffer-name nil)
  160.  
  161. (defvar vc-log-file)
  162. (defvar vc-log-version)
  163.  
  164. (defconst vc-name-assoc-file "VC-names")
  165.  
  166. (defvar vc-dired-mode nil)
  167. (make-variable-buffer-local 'vc-dired-mode)
  168.  
  169. (defvar vc-comment-ring nil)
  170. (defvar vc-comment-ring-index nil)
  171. (defvar vc-last-comment-match nil)
  172.  
  173. ;; File property caching
  174.  
  175. (defun vc-file-clearprops (file)
  176.   ;; clear all properties of a given file
  177.   (setplist (intern file vc-file-prop-obarray) nil))
  178.  
  179. (defun vc-clear-context ()
  180.   "Clear all cached file properties and the comment ring."
  181.   (interactive)
  182.   (fillarray vc-file-prop-obarray nil)
  183.   ;; Note: there is potential for minor lossage here if there is an open
  184.   ;; log buffer with a nonzero local value of vc-comment-ring-index.
  185.   (setq vc-comment-ring nil))
  186.  
  187. ;; Random helper functions
  188.  
  189. (defun vc-registration-error (file)
  190.   (if file
  191.       (error "File %s is not under version control" file)
  192.     (error "Buffer %s is not associated with a file" (buffer-name))))
  193.  
  194. (defvar vc-binary-assoc nil)
  195.  
  196. (defun vc-find-binary (name)
  197.   "Look for a command anywhere on the subprocess-command search path."
  198.   (or (cdr (assoc name vc-binary-assoc))
  199.       ;; XEmacs - use locate-file
  200.       (let ((full (locate-file name exec-path nil 1)))
  201.     (if full
  202.         (setq vc-binary-assoc (cons (cons name full) vc-binary-assoc)))
  203.     full)))
  204.  
  205. (defun vc-do-command (okstatus command file last &rest flags)
  206.   "Execute a version-control command, notifying user and checking for errors.
  207. The command is successful if its exit status does not exceed OKSTATUS.
  208. Output from COMMAND goes to buffer *vc*.  The last argument of the command is
  209. the master name of FILE if LAST is 'MASTER, or the workfile of FILE if LAST is
  210. 'WORKFILE; this is appended to an optional list of FLAGS."
  211.   (setq file (expand-file-name file))
  212.   (let ((camefrom (current-buffer))
  213.     (pwd (file-name-directory (expand-file-name file)))
  214.     (squeezed nil)
  215.     (vc-file (and file (vc-name file)))
  216.     status)
  217. ;;; #### - don't know why this code was here...to beautify the echo message?
  218. ;;;       the version of code below doesn't break default-directory, but it
  219. ;;;       still might mess up CVS and RCS because they like to operate on
  220. ;;;       files in the current directory. --Stig
  221. ;;;
  222. ;;;     (if (string-match (concat "^" (regexp-quote pwd)) file)
  223. ;;;         (setq file (substring file (match-end 0)))
  224. ;;;       (setq pwd (file-name-directory file)))
  225.     (if vc-command-messages
  226.     (message "Running %s on %s..." command file))
  227.     (set-buffer (get-buffer-create "*vc*"))
  228.     (setq default-directory pwd
  229.       file (file-name-nondirectory file))
  230.  
  231.     (set (make-local-variable 'vc-parent-buffer) camefrom)
  232.     (set (make-local-variable 'vc-parent-buffer-name)
  233.      (concat " from " (buffer-name camefrom)))
  234.     
  235.     (erase-buffer)
  236.  
  237.     (mapcar
  238.      (function (lambda (s) (and s (setq squeezed (append squeezed (list s))))))
  239.      flags)
  240.     (if (and vc-file (eq last 'MASTER))
  241.     (setq squeezed (append squeezed (list vc-file))))
  242.     (if (eq last 'WORKFILE)
  243.     (setq squeezed (append squeezed (list file))))
  244.     (let ((exec-path (if vc-path (append vc-path exec-path) exec-path))
  245.       ;; Add vc-path to PATH for the execution of this command.
  246.       (process-environment (copy-sequence process-environment)))
  247.       (setenv "PATH" (mapconcat 'identity exec-path ":"))
  248.       (setq status (apply 'call-process command nil t nil squeezed)))
  249.     (goto-char (point-max))
  250.     (set-buffer-modified-p nil)        ; XEmacs - fsf uses `not-modified'
  251.     (forward-line -1)
  252.     (if (or (not (integerp status)) (< okstatus status))
  253.     (progn
  254.       (pop-to-buffer "*vc*")
  255.       (goto-char (point-min))
  256.       (shrink-window-if-larger-than-buffer)
  257.       (error "Running %s...FAILED (%s)" command
  258.          (if (integerp status)
  259.              (format "status %d" status)
  260.            status))
  261.       )
  262.       (if vc-command-messages
  263.       (message "Running %s...OK" command))
  264.       )
  265.     (set-buffer camefrom)
  266.     status)
  267.   )
  268.  
  269. ;;; Save a bit of the text around POSN in the current buffer, to help
  270. ;;; us find the corresponding position again later.  This works even
  271. ;;; if all markers are destroyed or corrupted.
  272. (defun vc-position-context (posn)
  273.   (list posn
  274.     (buffer-size)
  275.     (buffer-substring posn
  276.               (min (point-max) (+ posn 100)))))
  277.  
  278. ;;; Return the position of CONTEXT in the current buffer, or nil if we
  279. ;;; couldn't find it.
  280. (defun vc-find-position-by-context (context)
  281.   (let ((context-string (nth 2 context)))
  282.     (if (equal "" context-string)
  283.     (point-max)
  284.       (save-excursion
  285.     (let ((diff (- (nth 1 context) (buffer-size))))
  286.       (if (< diff 0) (setq diff (- diff)))
  287.       (goto-char (nth 0 context))
  288.       (if (or (search-forward context-string nil t)
  289.           ;; Can't use search-backward since the match may continue
  290.           ;; after point.
  291.           (progn (goto-char (- (point) diff (length context-string)))
  292.              ;; goto-char doesn't signal an error at
  293.              ;; beginning of buffer like backward-char would
  294.              (search-forward context-string nil t)))
  295.           ;; to beginning of OSTRING
  296.           (- (point) (length context-string))))))))
  297.  
  298. (defun vc-revert-buffer1 (&optional arg no-confirm)
  299.   ;; Most of this was shamelessly lifted from Sebastian Kremer's rcs.el mode.
  300.   ;; Revert buffer, try to keep point and mark where user expects them in spite
  301.   ;; of changes because of expanded version-control key words.
  302.   ;; This is quite important since otherwise typeahead won't work as expected.
  303.   (interactive "P")
  304.   (widen)
  305.   (let ((point-context (vc-position-context (point)))
  306.     ;; Use mark-marker to avoid confusion in transient-mark-mode.
  307.     ;; XEmacs - mark-marker t
  308.     (mark-context  (if (eq (marker-buffer (mark-marker t)) (current-buffer))
  309.                (vc-position-context (mark-marker t))))
  310.     ;; We may want to reparse the compilation buffer after revert
  311.     (reparse (and (boundp 'compilation-error-list) ;compile loaded
  312.               ;; Construct a list; each elt is nil or a buffer
  313.               ;; iff that buffer is a compilation output buffer
  314.               ;; that contains markers into the current buffer.
  315.               (save-excursion
  316.             (mapcar (function
  317.                  (lambda (buffer)
  318.                    (set-buffer buffer)
  319.                    (let ((errors (or
  320.                           compilation-old-error-list
  321.                           compilation-error-list))
  322.                      (buffer-error-marked-p nil))
  323.                      (while (and (consp errors)
  324.                          (not buffer-error-marked-p))
  325.                        (and (markerp (cdr (car errors)))
  326.                         (eq buffer
  327.                         (marker-buffer
  328.                          (cdr (car errors))))
  329.                         (setq buffer-error-marked-p t))
  330.                        (setq errors (cdr errors)))
  331.                      (if buffer-error-marked-p buffer))))
  332.                 (buffer-list))))))
  333.  
  334.     ;; The FSF version intentionally runs font-lock here.  That
  335.     ;; usually just leads to a correctly font-locked buffer being
  336.     ;; redone.  #### We should detect the cases where the font-locking
  337.     ;; may be incorrect (such as on reverts).  We know that it is fine
  338.     ;; during regular checkin and checkouts.
  339.  
  340.     ;; the actual revisit
  341.     (revert-buffer arg no-confirm)
  342.  
  343.     ;; Reparse affected compilation buffers.
  344.     (while reparse
  345.       (if (car reparse)
  346.       (save-excursion
  347.         (set-buffer (car reparse))
  348.         (let ((compilation-last-buffer (current-buffer)) ;select buffer
  349.           ;; Record the position in the compilation buffer of
  350.           ;; the last error next-error went to.
  351.           (error-pos (marker-position
  352.                   (car (car-safe compilation-error-list)))))
  353.           ;; Reparse the error messages as far as they were parsed before.
  354.           (compile-reinitialize-errors '(4) compilation-parsing-end)
  355.           ;; Move the pointer up to find the error we were at before
  356.           ;; reparsing.  Now next-error should properly go to the next one.
  357.           (while (and compilation-error-list
  358.               (/= error-pos (car (car compilation-error-list))))
  359.         (setq compilation-error-list (cdr compilation-error-list))))))
  360.       (setq reparse (cdr reparse)))
  361.  
  362.     ;; Restore point and mark
  363.     (let ((new-point (vc-find-position-by-context point-context)))
  364.       (if new-point (goto-char new-point)))
  365.     (if mark-context
  366.     (let ((new-mark (vc-find-position-by-context mark-context)))
  367.       (if new-mark (set-mark new-mark))))))
  368.  
  369.  
  370. (defun vc-buffer-sync (&optional not-urgent)
  371.   ;; Make sure the current buffer and its working file are in sync
  372.   ;; NOT-URGENT means it is ok to continue if the user says not to save.
  373.   (if (buffer-modified-p)
  374.       (if (or vc-suppress-confirm
  375.           (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name))))
  376.       (save-buffer)
  377.     (if not-urgent
  378.         nil
  379.       (error "Aborted")))))
  380.  
  381. ;;;###autoload
  382. (defun vc-file-status ()
  383.   "Display the current status of the file being visited.
  384. Currently, this is only defined for CVS.  The information provided in the
  385. modeline is generally sufficient for RCS and SCCS."
  386.   ;; by Stig@hackvan.com
  387.   (interactive) 
  388.   (vc-buffer-sync t)
  389.   (let ((type (vc-backend-deduce buffer-file-name))
  390.     (file buffer-file-name))
  391.     (cond ((null type)
  392.        (if buffer-file-name
  393.            (message "`%s' is not registered with a version control system."
  394.             buffer-file-name)
  395.          (ding)
  396.          (message "Buffer `%s' has no associated file."
  397.               (buffer-name (current-buffer)))))
  398.       ((eq 'CVS type)
  399.        (vc-do-command 0 "cvs" file 'WORKFILE "status" "-v")
  400.        (set-buffer "*vc*")
  401.        (set-buffer-modified-p nil)
  402.        ;; reparse the status information, since we have it handy...
  403.        (vc-parse-buffer '("Status: \\(.*\\)") file '(vc-cvs-status))
  404.        (goto-char (point-min))
  405.        (shrink-window-if-larger-than-buffer
  406.         (display-buffer (current-buffer))))
  407.       (t
  408.        (ding)
  409.        (message "Operation not yet defined for RCS or SCCS.")))
  410.     ))
  411.  
  412. (defun vc-workfile-unchanged-p (file &optional want-differences-if-changed)
  413.   ;; Has the given workfile changed since last checkout?
  414.   (cond ((and (eq 'CVS (vc-backend-deduce file))
  415.           (not want-differences-if-changed))
  416.  
  417.      (let ((status (vc-file-getprop file 'vc-cvs-status)))
  418.        ;; #### - should this have some kind of timeout?  how often does
  419.        ;; this get called?  possibly the cached information should be
  420.        ;; flushed out of hand.  The only concern is the VC menu, which
  421.        ;; may indirectly call this function.
  422.        (or status            ; #### - caching is error-prone
  423.            (setq status (car (vc-log-info "cvs" file 'WORKFILE '("status")
  424.                           '("Status: \\(.*\\)")
  425.                           '(vc-cvs-status)))))
  426.        (string= status "Up-to-date")))
  427.     (t 
  428.      (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
  429.            (lastmod (nth 5 (file-attributes file)))
  430.            unchanged)
  431.        (or (equal checkout-time lastmod)
  432.            (and (or (not checkout-time) want-differences-if-changed)
  433.             (setq unchanged
  434.               (zerop (vc-backend-diff file nil nil
  435.                           (not want-differences-if-changed))))
  436.             ;; 0 stands for an unknown time; it can't match any mod time.
  437.             (vc-file-setprop file 'vc-checkout-time (if unchanged lastmod 0))
  438.             unchanged))))))
  439.  
  440. (defun vc-next-action-on-file (file verbose &optional comment)
  441.   ;;; If comment is specified, it will be used as an admin or checkin comment.
  442.   (let ((vc-file (vc-name file))
  443.     (vc-type (vc-backend-deduce file))
  444.     owner version)
  445.     (cond
  446.  
  447.      ;; if there is no master file corresponding, create one
  448.      ((not vc-file)
  449.       (vc-register verbose comment))
  450.  
  451.      ;; if there is no lock on the file, assert one and get it
  452.      ((and (not (eq vc-type 'CVS))    ;There are no locks in CVS.
  453.        (not (setq owner (vc-locking-user file))))
  454.       (if (and vc-checkout-carefully
  455.            (not (vc-workfile-unchanged-p file t)))
  456.       (if (save-window-excursion
  457.         (pop-to-buffer "*vc*")
  458.         (goto-char (point-min))
  459.         (insert (format "Changes to %s since last lock:\n\n" file))
  460.         (not (beep))
  461.         (yes-or-no-p
  462.          "File has unlocked changes, claim lock retaining changes? "))
  463.           (progn (vc-backend-steal file)
  464.              (vc-mode-line file))
  465.         (if (not (yes-or-no-p "Revert to checked-in version, instead? "))
  466.         (error "Checkout aborted.")
  467.           (vc-revert-buffer1 t t)
  468.           (vc-checkout-writable-buffer file))
  469.         )
  470.     (vc-checkout-writable-buffer file)))
  471.  
  472.      ;; a checked-out version exists, but the user may not own the lock
  473.      ((and (not (eq vc-type 'CVS))    ;There are no locks in CVS.
  474.        (not (string-equal owner (user-login-name))))
  475.       (if comment
  476.       (error "Sorry, you can't steal the lock on %s this way" file))
  477.       (vc-steal-lock
  478.        file
  479.        (and verbose (read-string "Version to steal: "))
  480.        owner))
  481.  
  482.      ;; changes to the master file needs to be merged back into the
  483.      ;; working file
  484.      ((and (eq vc-type 'CVS)
  485.        ;; "0" means "added, but not yet committed"
  486.        (not (string= (vc-file-getprop file 'vc-your-latest-version) "0"))
  487.        (progn
  488.          (vc-fetch-properties file)
  489.          (not (string= (vc-file-getprop file 'vc-your-latest-version)
  490.                (vc-file-getprop file 'vc-latest-version)))))
  491.       (vc-buffer-sync)
  492.       (if (yes-or-no-p (format "%s is not up-to-date.  Merge in changes now? "
  493.                    (buffer-name)))
  494.       (progn
  495.         (if (and (buffer-modified-p)
  496.              (not (yes-or-no-p 
  497.                (format 
  498.                 "Buffer %s modified; merge file on disc anyhow? " 
  499.                 (buffer-name)))))
  500.         (error "Merge aborted"))
  501.         (if (not (zerop (vc-backend-merge-news file)))
  502.         ;; Overlaps detected - what now?  Should use some
  503.         ;; fancy RCS conflict resolving package, or maybe
  504.         ;; emerge, but for now, simply warn the user with a
  505.         ;; message.
  506.         (message "Conflicts detected!"))
  507.         (vc-resynch-window file t (not (buffer-modified-p))))
  508.  
  509.     (error "%s needs update" (buffer-name))))
  510.  
  511.      ((and buffer-read-only (eq vc-type 'CVS))
  512.       (toggle-read-only)
  513.       ;; Sites who make link farms to a read-only gold tree (or
  514.       ;; something similar) can use the hook below to break the
  515.       ;; sym-link.
  516.       (run-hooks 'vc-make-buffer-writable-hook))
  517.  
  518.      ;; OK, user owns the lock on the file (or we are running CVS)
  519.      (t
  520.       (find-file file)
  521.  
  522.       ;; give luser a chance to save before checking in.
  523.       (vc-buffer-sync)
  524.  
  525.       ;; Revert if file is unchanged and buffer is too.
  526.       ;; If buffer is modified, that means the user just said no
  527.       ;; to saving it; in that case, don't revert,
  528.       ;; because the user might intend to save
  529.       ;; after finishing the log entry.
  530.       (if (and (vc-workfile-unchanged-p file) 
  531.            (not (buffer-modified-p)))
  532.       (progn
  533.         (if (eq vc-type 'CVS)
  534.         (message "No changes to %s" file)
  535.  
  536.           (vc-backend-revert file)
  537.           ;; DO NOT revert the file without asking the user!
  538.           (vc-resynch-window file t nil)))
  539.  
  540.     ;; user may want to set nonstandard parameters
  541.     (if verbose
  542.         (setq version (read-string "New version level: ")))
  543.  
  544.     ;; OK, let's do the checkin
  545.     (vc-checkin file version comment)
  546.     )))))
  547.  
  548. (defun vc-next-action-dired (file rev comment)
  549.   ;; We've accepted a log comment, now do a vc-next-action using it on all
  550.   ;; marked files.
  551.   (set-buffer vc-parent-buffer)
  552.   (dired-map-over-marks
  553.    (save-window-excursion
  554.      (let ((file (dired-get-filename)))
  555.        (message "Processing %s..." file)
  556.        (vc-next-action-on-file file nil comment)
  557.        (message "Processing %s...done" file)))
  558.    nil t)
  559.   )
  560.  
  561. ;; Here's the major entry point.
  562.  
  563. ;;;###autoload
  564. (defun vc-next-action (verbose)
  565.   "Do the next logical checkin or checkout operation on the current file.
  566.  
  567. For RCS and SCCS files:
  568.    If the file is not already registered, this registers it for version
  569. control and then retrieves a writable, locked copy for editing.
  570.    If the file is registered and not locked by anyone, this checks out
  571. a writable and locked file ready for editing.
  572.    If the file is checked out and locked by the calling user, this
  573. first checks to see if the file has changed since checkout.  If not,
  574. it performs a revert.
  575.    If the file has been changed, this pops up a buffer for entry
  576. of a log message; when the message has been entered, it checks in the
  577. resulting changes along with the log message as change commentary.  If
  578. the variable `vc-keep-workfiles' is non-nil (which is its default), a
  579. read-only copy of the changed file is left in place afterwards.
  580.    If the file is registered and locked by someone else, you are given
  581. the option to steal the lock.
  582.  
  583. For CVS files:
  584.    If the file is not already registered, this registers it for version
  585. control.  This does a \"cvs add\", but no \"cvs commit\".
  586.    If the file is added but not committed, it is committed.
  587.    If the file has not been changed, neither in your working area or
  588. in the repository, a message is printed and nothing is done.
  589.    If your working file is changed, but the repository file is
  590. unchanged, this pops up a buffer for entry of a log message; when the
  591. message has been entered, it checks in the resulting changes along
  592. with the logmessage as change commentary.  A writable file is retained.
  593.    If the repository file is changed, you are asked if you want to
  594. merge in the changes into your working copy.
  595.  
  596. The following is true regardless of which version control system you
  597. are using:
  598.  
  599.    If you call this from within a VC dired buffer with no files marked,
  600. it will operate on the file in the current line.
  601.    If you call this from within a VC dired buffer, and one or more
  602. files are marked, it will accept a log message and then operate on
  603. each one.  The log message will be used as a comment for any register
  604. or checkin operations, but ignored when doing checkouts.  Attempted
  605. lock steals will raise an error.
  606.  
  607.    For checkin, a prefix argument lets you specify the version number to use."
  608.   (interactive "P")
  609.   (catch 'nogo
  610.     (if vc-dired-mode
  611.     (let ((files (dired-get-marked-files)))
  612.       (if (= (length files) 1)
  613.           (find-file-other-window (car files))
  614.         (vc-start-entry nil nil nil
  615.                 "Enter a change comment for the marked files."
  616.                 'vc-next-action-dired)
  617.         (throw 'nogo nil))))
  618.     (while vc-parent-buffer
  619.       (pop-to-buffer vc-parent-buffer))
  620.     (if buffer-file-name
  621.     (vc-next-action-on-file buffer-file-name verbose)
  622.       (vc-registration-error nil))))
  623.  
  624. ;;; These functions help the vc-next-action entry point
  625.  
  626. (defun vc-checkout-writable-buffer (&optional file)
  627.   "Retrieve a writable copy of the latest version of the current buffer's file."
  628.   (vc-checkout (or file (buffer-file-name)) t)
  629.   )
  630.  
  631. ;;;###autoload
  632. (defun vc-register (&optional override comment)
  633.   "Register the current file into your version-control system."
  634.   (interactive "P")
  635.   (let ((master (vc-name buffer-file-name)))
  636.     (and master (file-exists-p master)
  637.      (error "This file is already registered"))
  638.     (and master
  639.      (not (y-or-n-p "Previous master file has vanished.  Make a new one? "))
  640.      (error "This file is already registered")))
  641.   ;; Watch out for new buffers of size 0: the corresponding file
  642.   ;; does not exist yet, even though buffer-modified-p is nil.
  643.   (if (and (not (buffer-modified-p))
  644.        (zerop (buffer-size))
  645.        (not (file-exists-p buffer-file-name)))
  646.       (set-buffer-modified-p t))
  647.   (vc-buffer-sync)
  648.   (vc-admin
  649.    buffer-file-name
  650.    (and override
  651.     (read-string
  652.      (format "Initial version level for %s: " buffer-file-name))))
  653.   )
  654.  
  655. (defun vc-resynch-window (file &optional keep noquery)
  656.   ;; If the given file is in the current buffer,
  657.   ;; either revert on it so we see expanded keyworks,
  658.   ;; or unvisit it (depending on vc-keep-workfiles)
  659.   ;; NOQUERY if non-nil inhibits confirmation for reverting.
  660.   ;; NOQUERY should be t *only* if it is known the only difference
  661.   ;; between the buffer and the file is due to RCS rather than user editing!
  662.   (and (string= buffer-file-name file)
  663.        (if keep
  664.        (progn
  665.          (vc-revert-buffer1 t noquery)
  666.          (vc-mode-line buffer-file-name))
  667.      (progn
  668.        (delete-window)
  669.        (kill-buffer (current-buffer))))))
  670.  
  671. (defun vc-start-entry (file rev comment msg action &optional after-hook)
  672.   ;; Accept a comment for an operation on FILE revision REV.  If COMMENT
  673.   ;; is nil, pop up a VC-log buffer, emit MSG, and set the
  674.   ;; action on close to ACTION; otherwise, do action immediately.
  675.   ;; Remember the file's buffer in vc-parent-buffer (current one if no file).
  676.   ;; AFTER-HOOK specifies the local value for vc-log-operation-hook.
  677.   (let ((parent (if file (find-file-noselect file) (current-buffer))))
  678.     (if comment
  679.     (set-buffer (get-buffer-create "*VC-log*"))
  680.       (pop-to-buffer (get-buffer-create "*VC-log*")))
  681.     (set (make-local-variable 'vc-parent-buffer) parent)
  682.     (set (make-local-variable 'vc-parent-buffer-name)
  683.      (concat " from " (buffer-name vc-parent-buffer)))
  684.     (vc-mode-line (or file " (no file)"))
  685.     (vc-log-mode)
  686.     (make-local-variable 'vc-log-after-operation-hook)
  687.     (if after-hook
  688.     (setq vc-log-after-operation-hook after-hook))
  689.     (setq vc-log-operation action)
  690.     (setq vc-log-file file)
  691.     (setq vc-log-version rev)
  692.     (if comment
  693.     (progn
  694.       (erase-buffer)
  695.       (if (eq comment t)
  696.           (vc-finish-logentry t)
  697.         (insert comment)
  698.         (vc-finish-logentry nil)))
  699.       (message "%s  Type C-c C-c when done." msg))))
  700.  
  701. (defun vc-admin (file rev &optional comment)
  702.   "Check a file into your version-control system.
  703. FILE is the unmodified name of the file.  REV should be the base version
  704. level to check it in under.  COMMENT, if specified, is the checkin comment."
  705.   (vc-start-entry file rev
  706.           (or comment (not vc-initial-comment))
  707.           "Enter initial comment." 'vc-backend-admin
  708.           nil))
  709.  
  710. (defun vc-checkout (file &optional writable)
  711.   "Retrieve a copy of the latest version of the given file."
  712.   ;; XEmacs - ftp is suppressed by the check for a filename handler in
  713.   ;;          vc-registered, so this is needless surplussage
  714.   ;; If ftp is on this system and the name matches the ange-ftp format
  715.   ;; for a remote file, the user is trying something that won't work.
  716.   ;;   (if (and (string-match "^/[^/:]+:" file) (vc-find-binary "ftp"))
  717.   ;;       (error "Sorry, you can't check out files over FTP"))
  718.   (vc-backend-checkout file writable)
  719.   (if (string-equal file buffer-file-name)
  720.       (vc-resynch-window file t t))
  721.   )
  722.  
  723. (defun vc-steal-lock (file rev &optional owner)
  724.   "Steal the lock on the current workfile."
  725.   (let (file-description)
  726.     (if (not owner)
  727.     (setq owner (vc-locking-user file)))
  728.     (if rev
  729.     (setq file-description (format "%s:%s" file rev))
  730.       (setq file-description file))
  731.     (if (not (y-or-n-p (format "Take the lock on %s from %s? "
  732.                    file-description owner)))
  733.     (error "Steal cancelled"))
  734.     (pop-to-buffer (get-buffer-create "*VC-mail*"))
  735.     (setq default-directory (expand-file-name "~/"))
  736.     (auto-save-mode auto-save-default)
  737.     (mail-mode)
  738.     (erase-buffer)
  739.     (mail-setup owner (format "Stolen lock on %s" file-description) nil nil nil
  740.         (list (list 'vc-finish-steal file rev)))
  741.     (goto-char (point-max))
  742.     (insert
  743.      (format "I stole the lock on %s, " file-description)
  744.      (current-time-string)
  745.      ".\n")
  746.     (message "Please explain why you stole the lock.  Type C-c C-c when done.")))
  747.  
  748. ;; This is called when the notification has been sent.
  749. (defun vc-finish-steal (file version)
  750.   (vc-backend-steal file version)
  751.   (if (get-file-buffer file)
  752.       (save-excursion
  753.     (set-buffer (get-file-buffer file))
  754.     (vc-resynch-window file t t))))
  755.  
  756. (defun vc-checkin (file &optional rev comment)
  757.   "Check in the file specified by FILE.
  758. The optional argument REV may be a string specifying the new version level
  759. \(if nil increment the current level).  The file is either retained with write
  760. permissions zeroed, or deleted (according to the value of `vc-keep-workfiles').
  761. If the back-end is CVS, a writable workfile is always kept.
  762. COMMENT is a comment string; if omitted, a buffer is
  763. popped up to accept a comment."
  764.   (vc-start-entry file rev comment
  765.           "Enter a change comment." 'vc-backend-checkin
  766.           'vc-checkin-hook))
  767.  
  768. ;;; Here is a checkin hook that may prove useful to sites using the
  769. ;;; ChangeLog facility supported by Emacs.
  770. (defun vc-comment-to-change-log (&optional whoami file-name)
  771.   "Enter last VC comment into change log file for current buffer's file.
  772. Optional arg (interactive prefix) non-nil means prompt for user name and site.
  773. Second arg is file name of change log.  \
  774. If nil, uses `change-log-default-name'."
  775.   (interactive (if current-prefix-arg
  776.            (list current-prefix-arg
  777.              (prompt-for-change-log-name))))
  778.   ;; Make sure the defvar for add-log-current-defun-function has been executed
  779.   ;; before binding it.
  780.   (require 'add-log)
  781.   (let (                ; Extract the comment first so we get any error before doing anything.
  782.     (comment (ring-ref vc-comment-ring 0))
  783.     ;; Don't let add-change-log-entry insert a defun name.
  784.     (add-log-current-defun-function 'ignore)
  785.     end)
  786.     ;; Call add-log to do half the work.
  787.     (add-change-log-entry whoami file-name t t)
  788.     ;; Insert the VC comment, leaving point before it.
  789.     (setq end (save-excursion (insert comment) (point-marker)))
  790.     (if (looking-at "\\s *\\s(")
  791.     ;; It starts with an open-paren, as in "(foo): Frobbed."
  792.     ;; So remove the ": " add-log inserted.
  793.     (delete-char -2))
  794.     ;; Canonicalize the white space between the file name and comment.
  795.     (just-one-space)
  796.     ;; Indent rest of the text the same way add-log indented the first line.
  797.     (let ((indentation (current-indentation)))
  798.       (save-excursion
  799.     (while (< (point) end)
  800.       (forward-line 1)
  801.       (indent-to indentation))
  802.     (setq end (point))))
  803.     ;; Fill the inserted text, preserving open-parens at bol.
  804.     (let ((paragraph-separate (concat paragraph-separate "\\|^\\s *\\s("))
  805.       (paragraph-start (concat paragraph-start "\\|^\\s *\\s(")))
  806.       (beginning-of-line)
  807.       (fill-region (point) end))
  808.     ;; Canonicalize the white space at the end of the entry so it is
  809.     ;; separated from the next entry by a single blank line.
  810.     (skip-syntax-forward " " end)
  811.     (delete-char (- (skip-syntax-backward " ")))
  812.     (or (eobp) (looking-at "\n\n")
  813.     (insert "\n"))))
  814.  
  815.  
  816. (defun vc-finish-logentry (&optional nocomment)
  817.   "Complete the operation implied by the current log entry."
  818.   (interactive)
  819.   ;; Check and record the comment, if any.
  820.   (if (not nocomment)
  821.       (progn
  822.     (goto-char (point-max))
  823.     (if (not (bolp))
  824.         (newline))
  825.     ;; Comment too long?
  826.     (vc-backend-logentry-check vc-log-file)
  827.     ;; Record the comment in the comment ring
  828.     (if (null vc-comment-ring)
  829.         (setq vc-comment-ring (make-ring vc-maximum-comment-ring-size)))
  830.     (ring-insert vc-comment-ring (buffer-string))
  831.     ))
  832.   ;; Sync parent buffer in case the user modified it while editing the comment.
  833.   ;; But not if it is a vc-dired buffer.
  834.   (save-excursion
  835.     (set-buffer vc-parent-buffer)
  836.     (or vc-dired-mode
  837.     (vc-buffer-sync)))
  838.   ;; OK, do it to it
  839.   (if vc-log-operation
  840.       (save-excursion
  841.     (funcall vc-log-operation 
  842.          vc-log-file
  843.          vc-log-version
  844.          (buffer-string)))
  845.     (error "No log operation is pending"))
  846.   ;; save the vc-log-after-operation-hook of log buffer
  847.   (let ((after-hook vc-log-after-operation-hook))
  848.     ;; Return to "parent" buffer of this checkin and remove checkin window
  849.     (pop-to-buffer vc-parent-buffer)
  850.     (let ((logbuf (get-buffer "*VC-log*")))
  851.       (delete-windows-on logbuf)
  852.       (kill-buffer logbuf))
  853.     ;; Now make sure we see the expanded headers
  854.     (if buffer-file-name
  855.     (vc-resynch-window buffer-file-name vc-keep-workfiles t))
  856.     (run-hooks after-hook)))
  857.  
  858. ;; Code for access to the comment ring
  859.  
  860. (defun vc-previous-comment (arg)
  861.   "Cycle backwards through comment history."
  862.   (interactive "*p")
  863.   (let ((len (ring-length vc-comment-ring)))
  864.     (cond ((or (not len) (<= len 0))    ; XEmacs change from Barry Warsaw
  865.        (message "Empty comment ring")
  866.        (ding))
  867.       (t
  868.        (erase-buffer)
  869.        ;; Initialize the index on the first use of this command
  870.        ;; so that the first M-p gets index 0, and the first M-n gets
  871.        ;; index -1.
  872.        (if (null vc-comment-ring-index)
  873.            (setq vc-comment-ring-index
  874.              (if (> arg 0) -1
  875.                (if (< arg 0) 1 0))))
  876.        (setq vc-comment-ring-index
  877.          (mod (+ vc-comment-ring-index arg) len))
  878.        (message "%d" (1+ vc-comment-ring-index))
  879.        (insert (ring-ref vc-comment-ring vc-comment-ring-index))))))
  880.  
  881. (defun vc-next-comment (arg)
  882.   "Cycle forwards through comment history."
  883.   (interactive "*p")
  884.   (vc-previous-comment (- arg)))
  885.  
  886. (defun vc-comment-search-reverse (str)
  887.   "Searches backwards through comment history for substring match."
  888.   (interactive "sComment substring: ")
  889.   (if (string= str "")
  890.       (setq str vc-last-comment-match)
  891.     (setq vc-last-comment-match str))
  892.   (if (null vc-comment-ring-index)
  893.       (setq vc-comment-ring-index -1))
  894.   (let ((str (regexp-quote str))
  895.     (len (ring-length vc-comment-ring))
  896.     (n (1+ vc-comment-ring-index)))
  897.     (while (and (< n len) (not (string-match str (ring-ref vc-comment-ring n))))
  898.       (setq n (+ n 1)))
  899.     (cond ((< n len)
  900.        (vc-previous-comment (- n vc-comment-ring-index)))
  901.       (t (error "Not found")))))
  902.  
  903. (defun vc-comment-search-forward (str)
  904.   "Searches forwards through comment history for substring match."
  905.   (interactive "sComment substring: ")
  906.   (if (string= str "")
  907.       (setq str vc-last-comment-match)
  908.     (setq vc-last-comment-match str))
  909.   (if (null vc-comment-ring-index)
  910.       (setq vc-comment-ring-index 0))
  911.   (let ((str (regexp-quote str))
  912.     (n vc-comment-ring-index))
  913.     (while (and (>= n 0) (not (string-match str (ring-ref vc-comment-ring n))))
  914.       (setq n (- n 1)))
  915.     (cond ((>= n 0)
  916.        (vc-next-comment (- n vc-comment-ring-index)))
  917.       (t (error "Not found")))))
  918.  
  919. ;; Additional entry points for examining version histories
  920.  
  921. ;;;###autoload
  922. (defun vc-diff (historic &optional not-urgent)
  923.   "Display diffs between file versions.
  924. Normally this compares the current file and buffer with the most recent 
  925. checked in version of that file.  This uses no arguments.
  926. With a prefix argument, it reads the file name to use
  927. and two version designators specifying which versions to compare."
  928.   (interactive "P")
  929.   (if vc-dired-mode
  930.       (set-buffer (find-file-noselect (dired-get-filename))))
  931.   (while vc-parent-buffer
  932.     (pop-to-buffer vc-parent-buffer))
  933.   (if historic
  934.       (call-interactively 'vc-version-diff)
  935.     (if (or (null buffer-file-name) (null (vc-name buffer-file-name)))
  936.     (error
  937.      "There is no version-control master associated with this buffer"))
  938.     (let ((file buffer-file-name)
  939.       unchanged)
  940.       (or (and file (vc-name file))
  941.       (vc-registration-error file))
  942.       (vc-buffer-sync not-urgent)
  943.       (setq unchanged (vc-workfile-unchanged-p buffer-file-name))
  944.       (if unchanged
  945.       (message "No changes to %s since latest version." file)
  946.     (vc-backend-diff file)
  947.     ;; Ideally, we'd like at this point to parse the diff so that
  948.     ;; the buffer effectively goes into compilation mode and we
  949.     ;; can visit the old and new change locations via next-error.
  950.     ;; Unfortunately, this is just too painful to do.  The basic
  951.     ;; problem is that the `old' file doesn't exist to be
  952.     ;; visited.  This plays hell with numerous assumptions in
  953.     ;; the diff.el and compile.el machinery.
  954.     (pop-to-buffer "*vc*")
  955.     (setq default-directory (file-name-directory file))
  956.     (if (= 0 (buffer-size))
  957.         (progn
  958.           (setq unchanged t)
  959.           (message "No changes to %s since latest version." file))
  960.       (goto-char (point-min))
  961.       (shrink-window-if-larger-than-buffer)))
  962.       (not unchanged))))
  963.  
  964. ;;;###autoload
  965. (defun vc-version-diff (file rel1 rel2)
  966.   "For FILE, report diffs between two stored versions REL1 and REL2 of it.
  967. If FILE is a directory, generate diffs between versions for all registered
  968. files in or below it."
  969.   ;; XEmacs - better prompt  
  970.   (interactive "FFile or directory to diff: \nsOlder version (default is repository): \nsNewer version (default is workfile): ")
  971.   (if (string-equal rel1 "") (setq rel1 nil))
  972.   (if (string-equal rel2 "") (setq rel2 nil))
  973.   (if (file-directory-p file)
  974.       (let ((camefrom (current-buffer)))
  975.     (set-buffer (get-buffer-create "*vc-status*"))
  976.     (set (make-local-variable 'vc-parent-buffer) camefrom)
  977.     (set (make-local-variable 'vc-parent-buffer-name)
  978.          (concat " from " (buffer-name camefrom)))
  979.     (erase-buffer)
  980.     (insert "Diffs between "
  981.         (or rel1 "last version checked in")
  982.         " and "
  983.         (or rel2 "current workfile(s)")
  984.         ":\n\n")
  985.     (set-buffer (get-buffer-create "*vc*"))
  986.     (cd file)
  987.     (vc-file-tree-walk
  988.      (function (lambda (f)
  989.              (message "Looking at %s" f)
  990.              (and
  991.               (not (file-directory-p f))
  992.               (vc-registered f)
  993.               (vc-backend-diff f rel1 rel2)
  994.               (append-to-buffer "*vc-status*" (point-min) (point-max)))
  995.              )))
  996.     (pop-to-buffer "*vc-status*")
  997.     (insert "\nEnd of diffs.\n")
  998.     (goto-char (point-min))
  999.     (set-buffer-modified-p nil)
  1000.     )
  1001.     (if (zerop (vc-backend-diff file rel1 rel2))
  1002.     (message "No changes to %s between %s and %s." file rel1 rel2)
  1003.       (pop-to-buffer "*vc*"))))
  1004.  
  1005. ;;;###autoload
  1006. (defun vc-version-other-window (rev)
  1007.   "Visit version REV of the current buffer in another window.
  1008. If the current buffer is named `F', the version is named `F.~REV~'.
  1009. If `F.~REV~' already exists, it is used instead of being re-created."
  1010.   (interactive "sVersion to visit (default is latest version): ")
  1011.   (if vc-dired-mode
  1012.       (set-buffer (find-file-noselect (dired-get-filename))))
  1013.   (while vc-parent-buffer
  1014.     (pop-to-buffer vc-parent-buffer))
  1015.   (if (and buffer-file-name (vc-name buffer-file-name))
  1016.       (let* ((version (if (string-equal rev "")
  1017.               (vc-latest-version buffer-file-name)
  1018.             rev))
  1019.          (filename (concat buffer-file-name ".~" version "~")))
  1020.     (or (file-exists-p filename)
  1021.         (vc-backend-checkout buffer-file-name nil version filename))
  1022.     (find-file-other-window filename))
  1023.     (vc-registration-error buffer-file-name)))
  1024.  
  1025. ;; Header-insertion code
  1026.  
  1027. ;;;###autoload
  1028. (defun vc-insert-headers ()
  1029.   "Insert headers in a file for use with your version-control system.
  1030. Headers desired are inserted at the start of the buffer, and are pulled from
  1031. the variable `vc-header-alist'."
  1032.   (interactive)
  1033.   (if vc-dired-mode
  1034.       (find-file-other-window (dired-get-filename)))
  1035.   (while vc-parent-buffer
  1036.     (pop-to-buffer vc-parent-buffer))
  1037.   (save-excursion
  1038.     (save-restriction
  1039.       (widen)
  1040.       (if (or (not (vc-check-headers))
  1041.           (y-or-n-p "Version headers already exist.  Insert another set? "))
  1042.       (progn
  1043.         (let* ((delims (cdr (assq major-mode vc-comment-alist)))
  1044.            (comment-start-vc (or (car delims) comment-start "#"))
  1045.            (comment-end-vc (or (car (cdr delims)) comment-end ""))
  1046.            (hdstrings (cdr (assoc (vc-backend-deduce buffer-file-name)
  1047.                       vc-header-alist))))
  1048.           (mapcar #'(lambda (s)
  1049.               (insert comment-start-vc "\t" s "\t"
  1050.                   comment-end-vc "\n"))
  1051.               hdstrings)
  1052.           (if vc-static-header-alist
  1053.           (mapcar #'(lambda (f)
  1054.                   (if (and buffer-file-name
  1055.                        (string-match (car f) buffer-file-name))
  1056.                   (insert (format (cdr f) (car hdstrings)))))
  1057.               vc-static-header-alist))
  1058.           )
  1059.         )))))
  1060.  
  1061. ;; The VC directory submode.  Coopt Dired for this.
  1062. ;; All VC commands get mapped into logical equivalents.
  1063.  
  1064. ;; XEmacs
  1065. (defvar vc-dired-prefix-map (let ((map (make-sparse-keymap)))
  1066.                   (set-keymap-name map 'vc-dired-prefix-map)
  1067.                   (define-key map "\C-xv" vc-prefix-map)
  1068.                   map))
  1069.  
  1070. (or (not (boundp 'minor-mode-map-alist))
  1071.     (assq 'vc-dired-mode minor-mode-map-alist)
  1072.     (setq minor-mode-map-alist
  1073.       (cons (cons 'vc-dired-mode vc-dired-prefix-map)
  1074.         minor-mode-map-alist)))
  1075.  
  1076. (defun vc-dired-mode ()
  1077.   "The augmented Dired minor mode used in VC directory buffers.
  1078. All Dired commands operate normally.  Users currently locking listed files
  1079. are listed in place of the file's owner and group.
  1080. Keystrokes bound to VC commands will execute as though they had been called
  1081. on a buffer attached to the file named in the current Dired buffer line."
  1082.   (setq vc-dired-mode t)
  1083.   (setq vc-mode " under VC"))
  1084.  
  1085. (defun vc-dired-reformat-line (x)
  1086.   ;; Hack a directory-listing line, plugging in locking-user info in
  1087.   ;; place of the user and group info.  Should have the beneficial
  1088.   ;; side-effect of shortening the listing line.  Each call starts with
  1089.   ;; point immediately following the dired mark area on the line to be
  1090.   ;; hacked.
  1091.   ;;
  1092.   ;; Simplest possible one:
  1093.   ;; (insert (concat x "\t")))
  1094.   ;;
  1095.   ;; This code, like dired, assumes UNIX -l format.
  1096.   (forward-word 1)            ; skip over any extra field due to -ibs options
  1097.   (cond ((numberp x)            ; This hack is used by the CVS code.  See vc-locking-user.
  1098.      (cond
  1099.       ((re-search-forward "\\([0-9]+ \\)\\([^ ]+\\)\\( .*\\)" nil 0)
  1100.        (save-excursion
  1101.          (goto-char (match-beginning 2))
  1102.          (insert "(")
  1103.          (goto-char (1+ (match-end 2)))
  1104.          (insert ")")
  1105.          (delete-char (- 17 (- (match-end 2) (match-beginning 2))))
  1106.          (insert (substring "      " 0
  1107.                 (- 7 (- (match-end 2) (match-beginning 2)))))))))
  1108.     (t
  1109.      (if x (setq x (concat "(" x ")")))
  1110.      (if (re-search-forward "\\([0-9]+ \\).................\\( .*\\)" nil 0)
  1111.          (let ((rep (substring (concat x "                 ") 0 9)))
  1112.            (replace-match (concat "\\1" rep "\\2") t)))
  1113.      )))
  1114.  
  1115. ;;;###autoload
  1116. (defun vc-directory (dir verbose &optional nested)
  1117.   "Show version-control status of all files in the directory DIR.
  1118. If the second argument VERBOSE is non-nil, show all files;
  1119. otherwise show only files that current locked in the version control system.
  1120. Interactively, supply a prefix arg to make VERBOSE non-nil.
  1121.  
  1122. If the optional third argument NESTED is non-nil,
  1123. scan the entire tree of subdirectories of the current directory."
  1124.   (interactive "DVC status of directory: \nP")
  1125.   (let* (nonempty
  1126.      (dl (length dir))
  1127.      (filelist nil) (userlist nil)
  1128.      dired-buf
  1129.      (subfunction
  1130.       (function (lambda (f)
  1131.               (if (vc-registered f)
  1132.               (let ((user (vc-locking-user f)))
  1133.                 (and (or verbose user)
  1134.                  (setq filelist (cons (substring f dl) filelist))
  1135.                  (setq userlist (cons user userlist)))))))))
  1136.     (let ((default-directory dir))
  1137.       (if nested
  1138.       (vc-file-tree-walk subfunction)
  1139.     (vc-dir-all-files subfunction)))
  1140.     (save-excursion
  1141.       (dired (make-string-stringlist (cons dir (nreverse filelist)))
  1142.               dired-listing-switches) 
  1143.       (rename-buffer (generate-new-buffer-name "VC-DIRED"))
  1144.       (setq dired-buf (current-buffer))
  1145.       (setq nonempty (not (zerop (buffer-size)))))
  1146.     (if nonempty
  1147.     (progn
  1148.       (pop-to-buffer dired-buf)
  1149.       (vc-dired-mode)
  1150.       (goto-char (point-min))
  1151.       (setq buffer-read-only nil)
  1152.       (forward-line 1)        ; Skip header line
  1153.       (mapcar
  1154.        (function
  1155.         (lambda (x)
  1156.           (forward-char 2)        ; skip dired's mark area
  1157.           (vc-dired-reformat-line x)
  1158.           (forward-line 1)))    ; go to next line
  1159.        (nreverse userlist))
  1160.       (setq buffer-read-only t)
  1161.       (goto-char (point-min))
  1162.       )
  1163.       (message "No files are currently %s under %s"
  1164.            (if verbose "registered" "locked") default-directory))
  1165.     ))
  1166.  
  1167. (defun make-string-stringlist (stringlist)
  1168.   "Turn a list of strings into a string of space-delimited elements."
  1169.   (save-excursion
  1170.     (let ((tlist stringlist)
  1171.            (buf (generate-new-buffer "*stringlist*")))
  1172.       (set-buffer buf)
  1173.       (insert (car tlist))
  1174.       (setq tlist (cdr tlist))
  1175.       (while (not (null tlist))
  1176.          (setq s (car tlist))
  1177.          (insert s " ")
  1178.          (setq tlist (cdr tlist)))
  1179.       (setq string (buffer-string))
  1180.       (kill-this-buffer)
  1181.       string
  1182.       )))
  1183.  
  1184. ;; Named-configuration support for SCCS
  1185.  
  1186. (defun vc-add-triple (name file rev)
  1187.   (save-excursion
  1188.     (find-file (concat (vc-backend-subdirectory-name file) "/" vc-name-assoc-file))
  1189.     (goto-char (point-max))
  1190.     (insert name "\t:\t" file "\t" rev "\n")
  1191.     (basic-save-buffer)
  1192.     (kill-buffer (current-buffer))
  1193.     ))
  1194.  
  1195. (defun vc-record-rename (file newname)
  1196.   (save-excursion
  1197.     (find-file (concat (vc-backend-subdirectory-name file) "/" vc-name-assoc-file))
  1198.     (goto-char (point-min))
  1199.     ;; (replace-regexp (concat ":" (regexp-quote file) "$") (concat ":" newname))
  1200.     (while (re-search-forward (concat ":" (regexp-quote file) "$") nil t)
  1201.       (replace-match (concat ":" newname) nil nil))
  1202.     (basic-save-buffer)
  1203.     (kill-buffer (current-buffer))
  1204.     ))
  1205.  
  1206. (defun vc-lookup-triple (file name)
  1207.   ;; Return the numeric version corresponding to a named snapshot of file
  1208.   ;; If name is nil or a version number string it's just passed through
  1209.   (cond ((null name) name)
  1210.     ((let ((firstchar (aref name 0)))
  1211.        (and (>= firstchar ?0) (<= firstchar ?9)))
  1212.      name)
  1213.     (t
  1214.      (car (vc-master-info
  1215.            (concat (vc-backend-subdirectory-name file) "/" vc-name-assoc-file)
  1216.            (list (concat name "\t:\t" file "\t\\(.+\\)"))))
  1217.      )))
  1218.  
  1219. ;; Named-configuration entry points
  1220.  
  1221. (defun vc-locked-example ()
  1222.   ;; Return an example of why the current directory is not ready to be snapshot
  1223.   ;; or nil if no such example exists.
  1224.   (catch 'vc-locked-example
  1225.     (vc-file-tree-walk
  1226.      (function (lambda (f)
  1227.          (if (and (vc-registered f) (vc-locking-user f))
  1228.              (throw 'vc-locked-example f)))))
  1229.     nil))
  1230.  
  1231. ;;;###autoload
  1232. (defun vc-create-snapshot (name)
  1233.   "Make a snapshot called NAME.
  1234. The snapshot is made from all registered files at or below the current
  1235. directory.  For each file, the version level of its latest
  1236. version becomes part of the named configuration."
  1237.   (interactive "sNew snapshot name: ")
  1238.   (let ((locked (vc-locked-example)))
  1239.     (if locked
  1240.     (error "File %s is locked" locked)
  1241.       (vc-file-tree-walk
  1242.        (function (lambda (f) (and
  1243.                   (vc-name f)
  1244.                   (vc-backend-assign-name f name)))))
  1245.       )))
  1246.  
  1247. ;;;###autoload
  1248. (defun vc-retrieve-snapshot (name)
  1249.   "Retrieve the snapshot called NAME.
  1250. This function fails if any files are locked at or below the current directory
  1251. Otherwise, all registered files are checked out (unlocked) at their version
  1252. levels in the snapshot."
  1253.   (interactive "sSnapshot name to retrieve: ")
  1254.   (let ((locked (vc-locked-example)))
  1255.     (if locked
  1256.         (error "File %s is locked" locked)
  1257.       (vc-file-tree-walk
  1258.        (function (lambda (f) (and
  1259.                   (vc-name f)
  1260.                   (vc-error-occurred
  1261.                                (vc-backend-checkout f nil name))))))
  1262.       )))
  1263.  
  1264. ;; Miscellaneous other entry points
  1265.  
  1266. ;;;###autoload
  1267. (defun vc-print-log ()
  1268.   "List the change log of the current buffer in a window."
  1269.   (interactive)
  1270.   (if vc-dired-mode
  1271.       (set-buffer (find-file-noselect (dired-get-filename))))
  1272.   (while vc-parent-buffer
  1273.     (pop-to-buffer vc-parent-buffer))
  1274.   (if (and buffer-file-name (vc-name buffer-file-name))
  1275.       (let ((file buffer-file-name))
  1276.         (vc-backend-print-log file)
  1277.     (pop-to-buffer (get-buffer-create "*vc*"))
  1278.     (setq default-directory (file-name-directory file))
  1279.     (while (looking-at "=*\n")
  1280.       (delete-char (- (match-end 0) (match-beginning 0)))
  1281.       (forward-line -1))
  1282.     (goto-char (point-min))
  1283.     (if (looking-at "[\b\t\n\v\f\r ]+")
  1284.         (delete-char (- (match-end 0) (match-beginning 0))))
  1285.         (shrink-window-if-larger-than-buffer)
  1286.     )
  1287.     (vc-registration-error buffer-file-name)))
  1288.  
  1289. ;;;###autoload
  1290. (defun vc-revert-buffer ()
  1291.   "Revert the current buffer's file back to the latest checked-in version.
  1292. This asks for confirmation if the buffer contents are not identical
  1293. to that version.
  1294. If the back-end is CVS, this will give you the most recent revision of
  1295. the file on the branch you are editing."
  1296.   (interactive)
  1297.   (if vc-dired-mode
  1298.       (find-file-other-window (dired-get-filename)))
  1299.   (while vc-parent-buffer
  1300.     (pop-to-buffer vc-parent-buffer))
  1301.   (let ((file buffer-file-name)
  1302.     (obuf (current-buffer)) (changed (vc-diff nil t)))
  1303.     (if (and changed (or vc-suppress-confirm
  1304.              (not (yes-or-no-p "Discard changes? "))))
  1305.     (progn
  1306.       (delete-window)
  1307.       (error "Revert cancelled"))
  1308.       (set-buffer obuf))
  1309.     (if changed
  1310.     (delete-window))
  1311.     (vc-backend-revert file)
  1312.     (vc-resynch-window file t t)
  1313.     ))
  1314.  
  1315. ;;;###autoload
  1316. (defun vc-cancel-version (norevert)
  1317.   "Get rid of most recently checked in version of this file.
  1318. A prefix argument means do not revert the buffer afterwards."
  1319.   (interactive "P")
  1320.   (if vc-dired-mode
  1321.       (find-file-other-window (dired-get-filename)))
  1322.   (while vc-parent-buffer
  1323.     (pop-to-buffer vc-parent-buffer))
  1324.   (let* ((target (concat (vc-latest-version (buffer-file-name))))
  1325.      (yours (concat (vc-your-latest-version (buffer-file-name))))
  1326.      (prompt (if (string-equal yours target)
  1327.              "Remove your version %s from master? "
  1328.            "Version %s was not your change.  Remove it anyway? ")))
  1329.     (if (null (yes-or-no-p (format prompt target)))
  1330.     nil
  1331.       (vc-backend-uncheck (buffer-file-name) target)
  1332.       (if (or norevert
  1333.           (not (yes-or-no-p "Revert buffer to most recent remaining version? ")))
  1334.       (vc-mode-line (buffer-file-name))
  1335.     (vc-checkout (buffer-file-name) nil)))
  1336.     ))
  1337.  
  1338. ;;;###autoload
  1339. (defun vc-rename-file (old new)
  1340.   "Rename file OLD to NEW, and rename its master file likewise."
  1341.   (interactive "fVC rename file: \nFRename to: ")
  1342.   ;; There are several ways of renaming files under CVS 1.3, but they all
  1343.   ;; have serious disadvantages.  See the FAQ (available from think.com in
  1344.   ;; pub/cvs/).  I'd rather send the user an error, than do something he might
  1345.   ;; consider to be wrong.  When the famous, long-awaited rename database is
  1346.   ;; implemented things might change for the better.  This is unlikely to occur
  1347.   ;; until CVS 2.0 is released.  --ceder 1994-01-23 21:27:51
  1348.   (if (eq (vc-backend-deduce old) 'CVS)
  1349.       (error "Renaming files under CVS is dangerous and not supported in VC."))
  1350.   (let ((oldbuf (get-file-buffer old)))
  1351.     (if (and oldbuf (buffer-modified-p oldbuf))
  1352.     (error "Please save files before moving them"))
  1353.     (if (get-file-buffer new)
  1354.     (error "Already editing new file name"))
  1355.     (if (file-exists-p new)
  1356.     (error "New file already exists"))
  1357.     (let ((oldmaster (vc-name old)))
  1358.       (if oldmaster
  1359.       (progn
  1360.             (if (vc-locking-user old)
  1361.         (error "Please check in files before moving them"))
  1362.         (if (or (file-symlink-p oldmaster)
  1363.             ;; This had FILE, I changed it to OLD. -- rms.
  1364.             (file-symlink-p (vc-backend-subdirectory-name old)))
  1365.                 (error "This is not a safe thing to do in the presence of symbolic links"))
  1366.         (rename-file
  1367.              oldmaster
  1368.          (let ((backend (vc-backend-deduce old))
  1369.            (newdir (or (file-name-directory new) ""))
  1370.            (newbase (file-name-nondirectory new)))
  1371.            (catch 'found
  1372.          (mapcar
  1373.                   (function
  1374.            (lambda (s)
  1375.              (if (eq backend (cdr s))
  1376.              (let* ((newmaster (format (car s) newdir newbase))
  1377.                 (newmasterdir (file-name-directory newmaster)))
  1378.                (if (or (not newmasterdir)
  1379.                    (file-directory-p newmasterdir))
  1380.                    (throw 'found newmaster))))))
  1381.           vc-master-templates)
  1382.          (error "New file lacks a version control directory"))))))
  1383.       (if (or (not oldmaster) (file-exists-p old))
  1384.       (rename-file old new)))
  1385.     ;; ?? Renaming a file might change its contents due to keyword expansion.
  1386.     ;; We should really check out a new copy if the old copy was precisely equal
  1387.     ;; to some checked in version.  However, testing for this is tricky....
  1388.     (if oldbuf
  1389.         (save-excursion
  1390.       (set-buffer oldbuf)
  1391.       (set-visited-file-name new)
  1392.       (set-buffer-modified-p nil))))
  1393.   ;; This had FILE, I changed it to OLD. -- rms.
  1394.   (vc-backend-dispatch old
  1395.     (vc-record-rename old new)        ;SCCS
  1396.     ;; #### - This CAN kinda be done for both rcs and
  1397.     ;; cvs.  It needs to be implemented. -- Stig
  1398.     nil                    ;RCS
  1399.     nil                    ;CVS
  1400.     )
  1401.   )
  1402.  
  1403. ;;;###autoload
  1404. (defun vc-update-change-log (&rest args)
  1405.   "Find change log file and add entries from recent RCS logs.
  1406. The mark is left at the end of the text prepended to the change log.
  1407. With prefix arg of C-u, only find log entries for the current buffer's file.
  1408. With any numeric prefix arg, find log entries for all files currently visited.
  1409. Otherwise, find log entries for all registered files in the default directory.
  1410. From a program, any arguments are passed to the `rcs2log' script."
  1411.   (interactive
  1412.    (cond ((consp current-prefix-arg)    ;C-u
  1413.       (list buffer-file-name))
  1414.      (current-prefix-arg            ;Numeric argument.
  1415.       (let ((files nil)
  1416.         (buffers (buffer-list))
  1417.         file)
  1418.         (while buffers
  1419.           (setq file (buffer-file-name (car buffers)))
  1420.           (and file (vc-backend-deduce file)
  1421.                    (setq files (cons file files)))
  1422.           (setq buffers (cdr buffers)))
  1423.         files))
  1424.      (t
  1425.       (let ((RCS (concat default-directory "RCS")))
  1426.         (and (file-directory-p RCS)
  1427.          (mapcar (function
  1428.               (lambda (f)
  1429.                 (if (string-match "\\(.*\\),v$" f)
  1430.                 (substring f 0 (match-end 1))
  1431.                   f)))
  1432.              (directory-files RCS nil "...\\|^[^.]\\|^.[^.]")))))))
  1433.   (let ((odefault default-directory))
  1434.     (find-file-other-window (find-change-log))
  1435.     (barf-if-buffer-read-only)
  1436.     (vc-buffer-sync)
  1437.     (undo-boundary)
  1438.     (goto-char (point-min))
  1439.     (push-mark)
  1440.     (message "Computing change log entries...")
  1441.     (message "Computing change log entries... %s"
  1442.          (if (or (null args)
  1443.                      (eq 0 (apply 'call-process "rcs2log" nil t nil
  1444.                   "-n"
  1445.                   (user-login-name)
  1446.                   (user-full-name)
  1447.                   user-mail-address
  1448.                   (mapcar (function
  1449.                        (lambda (f)
  1450.                          (file-relative-name
  1451.                                               (if (file-name-absolute-p f)
  1452.                           f
  1453.                         (concat odefault f)))))
  1454.                       args))))
  1455.          "done" "failed"))))
  1456.  
  1457. ;; Functions for querying the master and lock files.
  1458.  
  1459. ;; XEmacs - use match-string instead...
  1460. ;; (defun vc-match-substring (bn)
  1461. ;;   (buffer-substring (match-beginning bn) (match-end bn)))
  1462.  
  1463. (defun vc-parse-buffer (patterns &optional file properties)
  1464.   ;; Each pattern is of the form:
  1465.   ;;    regex                ; subex is 1, and date-subex is 2 (or nil)
  1466.   ;;    (regex subex date-subex)
  1467.   ;;
  1468.   ;; Use PATTERNS to parse information out of the current buffer by matching
  1469.   ;; each REGEX in the list and the returning the string matched by SUBEX.
  1470.   ;; If a DATE-SUBEX is present, then the SUBEX from the match with the
  1471.   ;; highest value for DATE-SUBEX (string comparison is used) will be
  1472.   ;; returned.
  1473.   ;;
  1474.   ;; If FILE and PROPERTIES are given, the latter must be a list of
  1475.   ;; properties of the same length as PATTERNS; each property is assigned
  1476.   ;; the corresponding value.
  1477.   ;;
  1478.   (let (pattern regex subex date-subex latest-date val values date)
  1479.     (while (setq pattern (car patterns))
  1480.       (if (stringp pattern)
  1481.       (setq regex pattern
  1482.         subex 1
  1483.         date-subex (and (string-match "\\\\(.*\\\\(" regex) 2))
  1484.     (setq regex (car pattern)
  1485.           subex (nth 1 pattern)
  1486.           date-subex (nth 2 pattern)))
  1487.       (goto-char (point-min))
  1488.       (if date-subex
  1489.       (progn
  1490.         (setq latest-date "" val nil)
  1491.         (while (re-search-forward regex nil t)
  1492.           (setq date (match-string date-subex))
  1493.           (if (string< latest-date date)
  1494.           (setq latest-date date
  1495.             val (match-string subex))))
  1496.         val)
  1497.     ;; no date subex, so just take the first match...
  1498.     (setq val (and (re-search-forward regex nil t) (match-string subex))))
  1499.       (if file (vc-file-setprop file (car properties) val))
  1500.       (setq values (cons val values)
  1501.         patterns (cdr patterns)
  1502.         properties (cdr properties)))
  1503.     values
  1504.     ))
  1505.  
  1506. (defun vc-master-info (file fields &optional rfile properties)
  1507.   ;; Search for information in a master file.
  1508.   (if (and file (file-exists-p file))
  1509.       (save-excursion
  1510.     (let ((buf))
  1511.       (setq buf (create-file-buffer file))
  1512.       (set-buffer buf))
  1513.     (erase-buffer)
  1514.     (insert-file-contents file)
  1515.     (set-buffer-modified-p nil)
  1516.     (auto-save-mode nil)
  1517.     (prog1
  1518.         (vc-parse-buffer fields rfile properties)
  1519.       (kill-buffer (current-buffer)))
  1520.     )
  1521.     (if rfile
  1522.     (mapcar
  1523.      (function (lambda (p) (vc-file-setprop rfile p nil)))
  1524.      properties))
  1525.     )
  1526.   )
  1527.  
  1528. (defun vc-log-info (command file last flags patterns &optional properties)
  1529.   ;; Search for information in log program output
  1530.   (if (and file (file-exists-p file))
  1531.       (save-excursion
  1532.     (set-buffer (get-buffer-create "*vc*"))
  1533.     (apply 'vc-do-command 0 command file last flags)
  1534.         (set-buffer-modified-p nil)
  1535.     (prog1
  1536.         (vc-parse-buffer patterns file properties)
  1537.       (kill-buffer (current-buffer))
  1538.           )
  1539.     )
  1540.     (if file
  1541.     (mapcar
  1542.      (function (lambda (p) (vc-file-setprop file p nil)))
  1543.      properties))
  1544.     )
  1545.   )
  1546.  
  1547. (defun vc-locking-user (file)
  1548.   "Return the name of the person currently holding a lock on FILE.
  1549. Return nil if there is no such person.
  1550. Under CVS, a file is considered locked if it has been modified since it
  1551. was checked out...even though it may well be writable by you."
  1552.   (setq file (expand-file-name file))    ; use full pathname
  1553.   (cond ((eq (vc-backend-deduce file) 'CVS)
  1554.      (if (vc-workfile-unchanged-p file t)
  1555.          nil
  1556.        ;; XEmacs - ahead of the pack...
  1557.        (user-login-name (nth 2 (file-attributes file)))))
  1558.     (t
  1559.      ;; #### - this can probably be cleaned up as a result of the changes to
  1560.      ;; user-login-name...  
  1561.      (if (or (not vc-keep-workfiles)
  1562.          (eq vc-mistrust-permissions 't)
  1563.          (and vc-mistrust-permissions
  1564.               (funcall vc-mistrust-permissions (vc-backend-subdirectory-name
  1565.                             file))))
  1566.          (vc-true-locking-user file)
  1567.        ;; This implementation assumes that any file which is under version
  1568.        ;; control and has -rw-r--r-- is locked by its owner.  This is true
  1569.        ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
  1570.        ;; We have to be careful not to exclude files with execute bits on;
  1571.        ;; scripts can be under version control too.  Also, we must ignore
  1572.        ;; the group-read and other-read bits, since paranoid users turn them off.
  1573.        ;; This hack wins because calls to the very expensive vc-fetch-properties
  1574.        ;; function only have to be made if (a) the file is locked by someone
  1575.        ;; other than the current user, or (b) some untoward manipulation
  1576.        ;; behind vc's back has changed the owner or the `group' or `other'
  1577.        ;; write bits.
  1578.        (let ((attributes (file-attributes file)))
  1579.          (cond ((string-match ".r-..-..-." (nth 8 attributes))
  1580.             nil)
  1581.            ((and (= (nth 2 attributes) (user-uid))
  1582.              (string-match ".rw..-..-." (nth 8 attributes)))
  1583.             (user-login-name))
  1584.            (t
  1585.             (vc-true-locking-user file)))) ; #### - this looks recursive!!!
  1586.        ))))
  1587.  
  1588. (defun vc-true-locking-user (file)
  1589.   ;; The slow but reliable version
  1590.   (vc-fetch-properties file)
  1591.   (vc-file-getprop file 'vc-locking-user))
  1592.  
  1593. (defun vc-latest-version (file)
  1594.   ;; Return version level of the latest version of FILE
  1595.   (vc-fetch-properties file)
  1596.   (vc-file-getprop file 'vc-latest-version))
  1597.  
  1598. (defun vc-your-latest-version (file)
  1599.   ;; Return version level of the latest version of FILE checked in by you
  1600.   (vc-fetch-properties file)
  1601.   (vc-file-getprop file 'vc-your-latest-version))
  1602.  
  1603. ;; Collect back-end-dependent stuff here
  1604. ;;
  1605. ;; Everything eventually funnels through these functions.  To implement
  1606. ;; support for a new version-control system, add another branch to the
  1607. ;; vc-backend-dispatch macro and fill it in in each call.  The variable
  1608. ;; vc-master-templates in vc-hooks.el will also have to change.
  1609.  
  1610. (put 'vc-backend-dispatch 'lisp-indent-function 'defun)
  1611.  
  1612. (defmacro vc-backend-dispatch (f s r c)
  1613.   "Execute FORM1, FORM2 or FORM3 depending whether we're using SCCS, RCS or CVS.
  1614. If FORM3 is RCS, use FORM2 even if we are using CVS.  (CVS shares some code 
  1615. with RCS)."
  1616.   (list 'let (list (list 'type (list 'vc-backend-deduce f)))
  1617.     (list 'cond
  1618.           (list (list 'eq 'type (quote 'SCCS)) s) ; SCCS
  1619.           (list (list 'eq 'type (quote 'RCS)) r) ; RCS
  1620.           (list (list 'eq 'type (quote 'CVS)) ; CVS
  1621.             (if (eq c 'RCS) r c))
  1622.           )))
  1623.  
  1624. (defun vc-lock-file (file)
  1625.   ;; Generate lock file name corresponding to FILE
  1626.   (let ((master (vc-name file)))
  1627.     (and
  1628.      master
  1629.      (string-match "\\(.*/\\)s\\.\\(.*\\)" master)
  1630.      (concat
  1631.       (substring master (match-beginning 1) (match-end 1))
  1632.       "p."
  1633.       (substring master (match-beginning 2) (match-end 2))))))
  1634.  
  1635.  
  1636. (defun vc-fetch-properties (file)
  1637.   ;; Re-fetch all properties associated with the given file.
  1638.   ;; Currently these properties are:
  1639.   ;;   vc-locking-user
  1640.   ;;   vc-locked-version 
  1641.   ;;   vc-latest-version
  1642.   ;;   vc-your-latest-version
  1643.   ;;   vc-cvs-status (cvs only)
  1644.   (vc-backend-dispatch
  1645.     file
  1646.     ;; SCCS
  1647.     (progn
  1648.       (vc-master-info (vc-lock-file file)
  1649.               (list
  1650.                "^[^ ]+ [^ ]+ \\([^ ]+\\)"
  1651.                "^\\([^ ]+\\)")
  1652.               file
  1653.               '(vc-locking-user vc-locked-version))
  1654.       (vc-master-info (vc-name file)
  1655.               (list
  1656.                "^\001d D \\([^ ]+\\)"
  1657.                (concat "^\001d D \\([^ ]+\\) .* " 
  1658.                    (regexp-quote (user-login-name)) " ")
  1659.                )
  1660.               file
  1661.               '(vc-latest-version vc-your-latest-version))
  1662.       )
  1663.     ;; RCS
  1664.     (vc-log-info "rlog" file 'MASTER nil
  1665.          (list
  1666.           "^locks: strict\n\t\\([^:]+\\)"
  1667.           "^locks: strict\n\t[^:]+: \\(.+\\)"
  1668.           "^revision[\t ]+\\([0-9.]+\\).*\ndate: \\([ /0-9:]+\\);"
  1669.           (concat
  1670.            "^revision[\t ]+\\([0-9.]+\\)\n.*author: "
  1671.            (regexp-quote (user-login-name))
  1672.            ";"))
  1673.          '(vc-locking-user vc-locked-version
  1674.                    vc-latest-version vc-your-latest-version))
  1675.     ;; CVS
  1676.     ;; Don't fetch vc-locking-user and vc-locked-version here, since they
  1677.     ;; should always be nil anyhow.  Don't fetch vc-your-latest-version, since
  1678.     ;; that is done in vc-find-cvs-master.
  1679.     (vc-log-info
  1680.      "cvs" file 'WORKFILE '("status")
  1681.      ;; CVS 1.3 says "RCS Version:", other releases "RCS Revision:",
  1682.      ;; and CVS 1.4a1 says "Repository revision:".  The regexp below
  1683.      ;; matches much more, but because of the way vc-log-info is
  1684.      ;; implemented it is impossible to use additional groups.
  1685.      '(("\\(RCS Version\\|RCS Revision\\|Repository revision\\):[\t ]+\\([0-9.]+\\)" 2)
  1686.        "Status: \\(.*\\)")
  1687.      '(vc-latest-version
  1688.        vc-cvs-status))
  1689.     ))
  1690.  
  1691. (defun vc-backend-subdirectory-name (&optional file)
  1692.   ;; Where the master and lock files for the current directory are kept
  1693.   (symbol-name
  1694.    (or
  1695.     (and file (vc-backend-deduce file))
  1696.     vc-default-back-end
  1697.     (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))))
  1698.  
  1699. (defun vc-backend-admin (file &optional rev comment)
  1700.   ;; Register a file into the version-control system
  1701.   ;; Automatically retrieves a read-only version of the file with
  1702.   ;; keywords expanded if vc-keep-workfiles is non-nil, otherwise
  1703.   ;; it deletes the workfile.
  1704.   (vc-file-clearprops file)
  1705.   (or vc-default-back-end
  1706.       (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))
  1707.   (message "Registering %s..." file)
  1708.   (let ((backend
  1709.          (cond
  1710.       ((file-exists-p (vc-backend-subdirectory-name)) vc-default-back-end)
  1711.       ((file-exists-p "RCS") 'RCS)
  1712.       ((file-exists-p "SCCS") 'SCCS)
  1713.           ((file-exists-p "CVS") 'CVS)
  1714.       (t vc-default-back-end))))
  1715.     (cond ((eq backend 'SCCS)
  1716.        (vc-do-command 0 "admin" file 'MASTER ; SCCS
  1717.               (and rev (concat "-r" rev))
  1718.               "-fb"
  1719.               (concat "-i" file)
  1720.               (and comment (concat "-y" comment))
  1721.               (format
  1722.                (car (rassq 'SCCS vc-master-templates))
  1723.                (or (file-name-directory file) "")
  1724.                (file-name-nondirectory file)))
  1725.        (delete-file file)
  1726.        (if vc-keep-workfiles
  1727.                (vc-do-command 0 "get" file 'MASTER)))
  1728.       ((eq backend 'RCS)
  1729.        (vc-do-command 0 "ci" file 'MASTER ; RCS
  1730.               (concat (if vc-keep-workfiles "-u" "-r") rev)
  1731.               (and comment (concat "-t-" comment))
  1732.               file))
  1733.           ((eq backend 'CVS)
  1734.        ;; #### - should maybe check to see if the master file is
  1735.        ;; already in the repository...in which case we need to add the
  1736.        ;; appropriate branch tag and do  an update.
  1737.        ;; #### - note that adding a file is a 2 step process in CVS...
  1738.        (vc-do-command 0 "cvs" file 'WORKFILE "add")
  1739.        (vc-do-command 0 "cvs" file 'WORKFILE "commit"
  1740.               (and comment (not (string= comment ""))
  1741.                    (concat "-m" comment)))
  1742.        )))
  1743.   (message "Registering %s...done" file)
  1744.   )
  1745.  
  1746. (defun vc-backend-checkout (file &optional writable rev workfile)
  1747.   ;; Retrieve a copy of a saved version into a workfile
  1748.   (let ((filename (or workfile file)))
  1749.     (message "Checking out %s..." filename)
  1750.     (save-excursion
  1751.       ;; Change buffers to get local value of vc-checkin-switches.
  1752.       (set-buffer (or (get-file-buffer file) (current-buffer)))
  1753.       (vc-backend-dispatch
  1754.     file
  1755.     ;; SCCS
  1756.     (if workfile
  1757.         ;; Some SCCS implementations allow checking out directly to a
  1758.         ;; file using the -G option, but then some don't so use the
  1759.         ;; least common denominator approach and use the -p option
  1760.         ;; ala RCS.
  1761.         (let ((vc-modes (logior (file-modes (vc-name file))
  1762.                     (if writable 128 0)))
  1763.           (failed t))
  1764.           (unwind-protect
  1765.           (progn
  1766.             (apply 'vc-do-command
  1767.                0 "/bin/sh" file 'MASTER "-c"
  1768.                ;; Some shells make the "" dummy argument into $0
  1769.                ;; while others use the shell's name as $0 and
  1770.                ;; use the "" as $1.  The if-statement
  1771.                ;; converts the latter case to the former.
  1772.                (format "if [ x\"$1\" = x ]; then shift; fi; \
  1773.                               umask %o; exec >\"$1\" || exit; \
  1774.                                shift; umask %o; exec get \"$@\""
  1775.                    (logand 511 (lognot vc-modes))
  1776.                    (logand 511 (lognot (default-file-modes))))
  1777.                ""        ; dummy argument for shell's $0
  1778.                filename 
  1779.                (if writable "-e")
  1780.                "-p" (and rev
  1781.                      (concat "-r" (vc-lookup-triple file rev)))
  1782.                vc-checkout-switches)
  1783.             (setq failed nil))
  1784.         (and failed (file-exists-p filename) (delete-file filename))))
  1785.       (apply 'vc-do-command 0 "get" file 'MASTER ; SCCS
  1786.          (if writable "-e")
  1787.          (and rev (concat "-r" (vc-lookup-triple file rev)))
  1788.          vc-checkout-switches))
  1789.     ;; RCS
  1790.     (if workfile
  1791.         ;; RCS doesn't let us check out into arbitrary file names directly.
  1792.         ;; Use `co -p' and make stdout point to the correct file.
  1793.         (let ((vc-modes (logior (file-modes (vc-name file))
  1794.                     (if writable 128 0)))
  1795.           (failed t))
  1796.           (unwind-protect
  1797.           (progn
  1798.             (apply 'vc-do-command
  1799.                0 "/bin/sh" file 'MASTER "-c"
  1800.                ;; See the SCCS case, above, regarding the
  1801.                ;; if-statement.
  1802.                (format "if [ x\"$1\" = x ]; then shift; fi; \
  1803.                               umask %o; exec >\"$1\" || exit; \
  1804.                                shift; umask %o; exec co \"$@\""
  1805.                    (logand 511 (lognot vc-modes))
  1806.                    (logand 511 (lognot (default-file-modes))))
  1807.                ""        ; dummy argument for shell's $0
  1808.                filename
  1809.                (if writable "-l")
  1810.                (concat "-p" rev)
  1811.                vc-checkout-switches)
  1812.             (setq failed nil))
  1813.         (and failed (file-exists-p filename) (delete-file filename))))
  1814.       (apply 'vc-do-command 0 "co" file 'MASTER
  1815.          (if writable "-l")
  1816.          (and rev (concat "-r" rev))
  1817.          vc-checkout-switches))
  1818.     ;; CVS
  1819.     (if workfile
  1820.         ;; CVS is much like RCS
  1821.         (let ((failed t))
  1822.           (unwind-protect
  1823.           (progn
  1824.             (apply 'vc-do-command
  1825.                0 "/bin/sh" file 'WORKFILE "-c"
  1826.                "exec >\"$1\" || exit; shift; exec cvs update \"$@\""
  1827.                ""        ; dummy argument for shell's $0
  1828.                workfile
  1829.                (concat "-r" rev)
  1830.                "-p"
  1831.                vc-checkout-switches)
  1832.             (setq failed nil))
  1833.         (and failed (file-exists-p filename) (delete-file filename))))
  1834.       (apply 'vc-do-command 0 "cvs" file 'WORKFILE
  1835.          "update"
  1836.          (and rev (concat "-r" rev))
  1837.          file
  1838.          vc-checkout-switches))
  1839.     ))
  1840.     (or workfile
  1841.     (vc-file-setprop file
  1842.              'vc-checkout-time (nth 5 (file-attributes file))))
  1843.     (message "Checking out %s...done" filename))
  1844.   )
  1845.  
  1846. (defun vc-backend-logentry-check (file)
  1847.   (vc-backend-dispatch file
  1848.     (if (>= (buffer-size) 512)        ; SCCS
  1849.     (progn
  1850.       (goto-char 512)
  1851.       (error
  1852.        "Log must be less than 512 characters; point is now at pos 512")))
  1853.     nil                    ; RCS
  1854.     nil)                ; CVS
  1855.   )
  1856.  
  1857. (defun vc-backend-checkin (file rev comment)
  1858.   ;; Register changes to FILE as level REV with explanatory COMMENT.
  1859.   ;; Automatically retrieves a read-only version of the file with
  1860.   ;; keywords expanded if vc-keep-workfiles is non-nil, otherwise
  1861.   ;; it deletes the workfile.
  1862.   (message "Checking in %s..." file)
  1863.   (save-excursion
  1864.     ;; Change buffers to get local value of vc-checkin-switches.
  1865.     (set-buffer (or (get-file-buffer file) (current-buffer)))
  1866.     (vc-backend-dispatch file
  1867.       (progn
  1868.     (apply 'vc-do-command 0 "delta" file 'MASTER
  1869.            (if rev (concat "-r" rev))
  1870.            (concat "-y" comment)
  1871.            vc-checkin-switches)
  1872.     (if vc-keep-workfiles
  1873.         (vc-do-command 0 "get" file 'MASTER))
  1874.     )
  1875.       (apply 'vc-do-command 0 "ci" file 'MASTER
  1876.          (concat (if vc-keep-workfiles "-u" "-r") rev)
  1877.          (if (not (string-equal "" comment))
  1878.          (concat "-m" comment))
  1879.          vc-checkin-switches)
  1880.       (progn
  1881.     (apply 'vc-do-command 0 "cvs" file 'WORKFILE 
  1882.            "ci"
  1883.            (if (not (string-equal "" comment))
  1884.            (list "-m" comment))
  1885.            vc-checkin-switches)
  1886.     (vc-file-setprop file 'vc-checkout-time 
  1887.              (nth 5 (file-attributes file))))
  1888.       ))
  1889.   (vc-file-setprop file 'vc-locking-user nil)
  1890.   (message "Checking in %s...done" file)
  1891.   )
  1892.  
  1893. (defun vc-backend-revert (file)
  1894.   ;; Revert file to latest checked-in version.
  1895.   (message "Reverting %s..." file)
  1896.   (vc-backend-dispatch
  1897.     file
  1898.     (progn                ; SCCS
  1899.       (vc-do-command 0 "unget" file 'MASTER nil)
  1900.       (vc-do-command 0 "get" file 'MASTER nil))
  1901.     (vc-do-command 0 "co" file 'MASTER    ; RCS.  This deletes the work file.
  1902.            "-f" "-u")
  1903.     (progn                ; CVS
  1904.       (delete-file file)
  1905.       (vc-do-command 0 "cvs" file 'WORKFILE "update"))
  1906.     )
  1907.   (vc-file-setprop file 'vc-locking-user nil)
  1908.   (message "Reverting %s...done" file)
  1909.   )
  1910.  
  1911. (defun vc-backend-steal (file &optional rev)
  1912.   ;; Steal the lock on the current workfile.  Needs RCS 5.6.2 or later for -M.
  1913.   (message "Stealing lock on %s..." file)
  1914.   (vc-backend-dispatch file
  1915.     (progn                ; SCCS
  1916.       (vc-do-command 0 "unget" file 'MASTER "-n" (if rev (concat "-r" rev)))
  1917.       (vc-do-command 0 "get" file 'MASTER "-g" (if rev (concat "-r" rev)))
  1918.       )
  1919.     (vc-do-command 0 "rcs" file 'MASTER ; RCS
  1920.            "-M" (concat "-u" rev) (concat "-l" rev))
  1921.     (error "You cannot steal a CVS lock; there are no CVS locks to steal.") ; CVS
  1922.     )
  1923.   (vc-file-setprop file 'vc-locking-user (user-login-name))
  1924.   (message "Stealing lock on %s...done" file)
  1925.   )  
  1926.  
  1927. (defun vc-backend-uncheck (file target)
  1928.   ;; Undo the latest checkin.  Note: this code will have to get a lot
  1929.   ;; smarter when we support multiple branches.
  1930.   (message "Removing last change from %s..." file)
  1931.   (vc-backend-dispatch file
  1932.     (vc-do-command 0 "rmdel" file 'MASTER (concat "-r" target))
  1933.     (vc-do-command 0 "rcs" file 'MASTER (concat "-o" target))
  1934.     (error "Unchecking files under CVS is dangerous and not supported in VC.")
  1935.     )
  1936.   (message "Removing last change from %s...done" file)
  1937.   )
  1938.  
  1939. (defun vc-backend-print-log (file)
  1940.   ;; Print change log associated with FILE to buffer *vc*.
  1941.   (vc-backend-dispatch 
  1942.     file
  1943.     (vc-do-command 0 "prs" file 'MASTER)
  1944.     (vc-do-command 0 "rlog" file 'MASTER)
  1945.     (vc-do-command 0 "cvs" file 'WORKFILE "rlog")))
  1946.  
  1947. (defun vc-backend-assign-name (file name)
  1948.   ;; Assign to a FILE's latest version a given NAME.
  1949.   (vc-backend-dispatch file
  1950.     (vc-add-triple name file (vc-latest-version file)) ; SCCS
  1951.     (vc-do-command 0 "rcs" file 'MASTER (concat "-n" name ":")) ; RCS
  1952.     (vc-do-command 0 "cvs" file 'WORKFILE "tag" name) ; CVS
  1953.     )
  1954.   )
  1955.  
  1956. (defun vc-backend-diff (file &optional oldvers newvers cmp)
  1957.   ;; Get a difference report between two versions of FILE.
  1958.   ;; Get only a brief comparison report if CMP, a difference report otherwise.
  1959.   (let ((backend (vc-backend-deduce file)))
  1960.     (cond
  1961.      ((eq backend 'SCCS)
  1962.       (setq oldvers (vc-lookup-triple file oldvers))
  1963.       (setq newvers (vc-lookup-triple file newvers))))
  1964.     (cond
  1965.      ;; SCCS and RCS shares a lot of code.
  1966.      ((or (eq backend 'SCCS) (eq backend 'RCS))
  1967.       (let* ((command (if (eq backend 'SCCS)
  1968.               "vcdiff"
  1969.             "rcsdiff"))
  1970.          (mode (if (eq backend 'RCS) 'WORKFILE 'MASTER))
  1971.          (options (append (list (and cmp "--brief")
  1972.                     "-q"
  1973.                                     (and oldvers (concat "-r" oldvers))
  1974.                     (and newvers (concat "-r" newvers)))
  1975.                               (and (not cmp)
  1976.                    (if (listp diff-switches)
  1977.                        diff-switches
  1978.                      (list diff-switches)))))
  1979.          (status (apply 'vc-do-command 2 command file mode options)))
  1980.     ;; Some RCS versions don't understand "--brief"; work around this.
  1981.     (if (eq status 2)
  1982.         (apply 'vc-do-command 1 command file 'WORKFILE
  1983.            (if cmp (cdr options) options))
  1984.       status)))
  1985.      ;; CVS is different.  
  1986.      ;; cmp is not yet implemented -- we always do a full diff.
  1987.      ((eq backend 'CVS)
  1988.       (if (string= (vc-file-getprop file 'vc-your-latest-version) "0") ; CVS
  1989.       ;; This file is added but not yet committed; there is no master file.
  1990.       ;; diff it against /dev/null.
  1991.       (if (or oldvers newvers)
  1992.           (error "No revisions of %s exists" file)
  1993.         (apply 'vc-do-command
  1994.            1 "diff" file 'WORKFILE "/dev/null"
  1995.            (if (listp diff-switches)
  1996.                diff-switches
  1997.              (list diff-switches))))
  1998.     (apply 'vc-do-command
  1999.            1 "cvs" file 'WORKFILE "diff"
  2000.            (and oldvers (concat "-r" oldvers))
  2001.            (and newvers (concat "-r" newvers))
  2002.            (if (listp diff-switches)
  2003.                    diff-switches
  2004.                  (list diff-switches)))))
  2005.      (t
  2006.       (vc-registration-error file)))))
  2007.  
  2008. (defun vc-backend-merge-news (file)
  2009.   ;; Merge in any new changes made to FILE.
  2010.   (vc-backend-dispatch 
  2011.     file
  2012.     (error "vc-backend-merge-news not meaningful for SCCS files") ; SCCS
  2013.     (error "vc-backend-merge-news not meaningful for RCS files") ; RCS
  2014.     (vc-do-command 1 "cvs" file 'WORKFILE "update") ; CVS
  2015.     ))
  2016.  
  2017. (defun vc-check-headers ()
  2018.   "Check if the current file has any headers in it."
  2019.   (interactive)
  2020.   (save-excursion
  2021.     (goto-char (point-min))
  2022.     (vc-backend-dispatch buffer-file-name
  2023.       (re-search-forward  "%[MIRLBSDHTEGUYFPQCZWA]%" nil t) ; SCCS
  2024.       (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t) ; RCS
  2025.       'RCS ; CVS works like RCS in this regard.
  2026.       )
  2027.     ))
  2028.  
  2029. ;; Back-end-dependent stuff ends here.
  2030.  
  2031. ;; Set up key bindings for use while editing log messages
  2032.  
  2033. (defun vc-log-mode ()
  2034.   "Minor mode for driving version-control tools.
  2035. These bindings are added to the global keymap when you enter this mode:
  2036. \\[vc-next-action]          perform next logical version-control operation on current file
  2037. \\[vc-register]                   register current file
  2038. \\[vc-toggle-read-only]            like next-action, but won't register files
  2039. \\[vc-insert-headers]         insert version-control headers in current file
  2040. \\[vc-print-log]          display change history of current file
  2041. \\[vc-revert-buffer]              revert buffer to latest version
  2042. \\[vc-cancel-version]            undo latest checkin
  2043. \\[vc-diff]          show diffs between file versions
  2044. \\[vc-version-other-window]             visit old version in another window
  2045. \\[vc-directory]             show all files locked by any user in or below .
  2046. \\[vc-update-change-log]         add change log entry from recent checkins
  2047.  
  2048. While you are entering a change log message for a version, the following
  2049. additional bindings will be in effect.
  2050.  
  2051. \\[vc-finish-logentry]   proceed with check in, ending log message entry
  2052.  
  2053. Whenever you do a checkin, your log comment is added to a ring of
  2054. saved comments.  These can be recalled as follows:
  2055.  
  2056. \\[vc-next-comment]   replace region with next message in comment ring
  2057. \\[vc-previous-comment] replace region with previous message in comment ring
  2058. \\[vc-comment-search-reverse]       search backward for regexp in the comment ring
  2059. \\[vc-comment-search-forward]     search backward for regexp in the comment ring
  2060.  
  2061. Entry to the change-log submode calls the value of text-mode-hook, then
  2062. the value of vc-log-mode-hook.
  2063.  
  2064. Global user options:
  2065.  vc-initial-comment      If non-nil, require user to enter a change
  2066.              comment upon first checkin of the file.
  2067.  
  2068.  vc-keep-workfiles       Non-nil value prevents workfiles from being
  2069.              deleted when changes are checked in
  2070.  
  2071.  vc-suppress-confirm     Suppresses some confirmation prompts,
  2072.              notably for reversions.
  2073.  
  2074.  vc-header-alist         Which keywords to insert when adding headers
  2075.              with \\[vc-insert-headers].  Defaults to
  2076.              '(\"\%\W\%\") under SCCS, '(\"\$Id\$\") under 
  2077.                          RCS and CVS.
  2078.  
  2079.  vc-static-header-alist  By default, version headers inserted in C files
  2080.                          get stuffed in a static string area so that
  2081.              ident(RCS/CVS) or what(SCCS) can see them in
  2082.              the compiled object code.  You can override
  2083.              this by setting this variable to nil, or change
  2084.                          the header template by changing it.
  2085.  
  2086.  vc-command-messages     if non-nil, display run messages from the
  2087.              actual version-control utilities (this is
  2088.              intended primarily for people hacking vc
  2089.              itself).
  2090. "
  2091.   (interactive)
  2092.   (set-syntax-table text-mode-syntax-table)
  2093.   (use-local-map vc-log-entry-mode)
  2094.   (setq local-abbrev-table text-mode-abbrev-table)
  2095.   (setq major-mode 'vc-log-mode)
  2096.   (setq mode-name "VC-Log")
  2097.   (make-local-variable 'vc-log-file)
  2098.   (make-local-variable 'vc-log-version)
  2099.   (make-local-variable 'vc-comment-ring-index)
  2100.   (set-buffer-modified-p nil)
  2101.   (setq buffer-file-name nil)
  2102.   (run-hooks 'text-mode-hook 'vc-log-mode-hook)
  2103.   )
  2104.  
  2105. ;; Initialization code, to be done just once at load-time
  2106. (if vc-log-entry-mode
  2107.     nil
  2108.   (setq vc-log-entry-mode (make-sparse-keymap))
  2109.   (set-keymap-name vc-log-entry-mode 'vc-log-entry-mode) ; XEmacs
  2110.   (define-key vc-log-entry-mode "\M-n" 'vc-next-comment)
  2111.   (define-key vc-log-entry-mode "\M-p" 'vc-previous-comment)
  2112.   (define-key vc-log-entry-mode "\M-r" 'vc-comment-search-reverse)
  2113.   (define-key vc-log-entry-mode "\M-s" 'vc-comment-search-forward)
  2114.   (define-key vc-log-entry-mode "\C-c\C-c" 'vc-finish-logentry)
  2115.   )
  2116.  
  2117. ;;; These things should probably be generally available
  2118.  
  2119. (defun vc-file-tree-walk (func &rest args)
  2120.   "Walk recursively through default directory.
  2121. Invoke FUNC f ARGS on each non-directory file f underneath it."
  2122.   (vc-file-tree-walk-internal default-directory func args)
  2123.   (message "Traversing directory %s...done" default-directory))
  2124.  
  2125. (defun vc-file-tree-walk-internal (file func args)
  2126.   (if (not (file-directory-p file))
  2127.       (apply func file args)
  2128.     (message "Traversing directory %s..." file)
  2129.     (let ((dir (file-name-as-directory file)))
  2130.       (mapcar
  2131.        (function
  2132.     (lambda (f) (or
  2133.              (string-equal f ".")
  2134.              (string-equal f "..")
  2135.              (member f vc-directory-exclusion-list)
  2136.              (let ((dirf (concat dir f)))
  2137.                        (or
  2138.             (file-symlink-p dirf) ; Avoid possible loops
  2139.             (vc-file-tree-walk-internal dirf func args))))))
  2140.        (directory-files dir)))))
  2141.  
  2142. (defun vc-dir-all-files (func &rest args)
  2143.   "Invoke FUNC f ARGS on each regular file f in default directory."
  2144.   (let ((dir default-directory))
  2145.     (message "Scanning directory %s..." dir)
  2146.     (mapcar (function (lambda (f)
  2147.             (let ((dirf (expand-file-name f dir)))
  2148.               (if (not (file-directory-p dirf))
  2149.                   (apply func dirf args)))))
  2150.             (directory-files dir))
  2151.     (message "Scanning directory %s...done" dir)))
  2152.  
  2153. (provide 'vc)
  2154.  
  2155. ;;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
  2156. ;;;
  2157. ;;; These may be useful to anyone who has to debug or extend the package.
  2158. ;;; 
  2159. ;;; A fundamental problem in VC is that there are time windows between
  2160. ;;; vc-next-action's computations of the file's version-control state and
  2161. ;;; the actions that change it.  This is a window open to lossage in a
  2162. ;;; multi-user environment; someone else could nip in and change the state
  2163. ;;; of the master during it.
  2164. ;;; 
  2165. ;;; The performance problem is that rlog/prs calls are very expensive; we want
  2166. ;;; to avoid them as much as possible.
  2167. ;;; 
  2168. ;;; ANALYSIS:
  2169. ;;; 
  2170. ;;; The performance problem, it turns out, simplifies in practice to the
  2171. ;;; problem of making vc-locking-user fast.  The two other functions that call
  2172. ;;; prs/rlog will not be so commonly used that the slowdown is a problem; one
  2173. ;;; makes snapshots, the other deletes the calling user's last change in the
  2174. ;;; master.
  2175. ;;; 
  2176. ;;; The race condition implies that we have to either (a) lock the master
  2177. ;;; during the entire execution of vc-next-action, or (b) detect and
  2178. ;;; recover from errors resulting from dispatch on an out-of-date state.
  2179. ;;; 
  2180. ;;; Alternative (a) appears to be unfeasible.  The problem is that we can't
  2181. ;;; guarantee that the lock will ever be removed.  Suppose a user starts a
  2182. ;;; checkin, the change message buffer pops up, and the user, having wandered
  2183. ;;; off to do something else, simply forgets about it?
  2184. ;;; 
  2185. ;;; Alternative (b), on the other hand, works well with a cheap way to speed up
  2186. ;;; vc-locking-user.  Usually, if a file is registered, we can read its locked/
  2187. ;;; unlocked state and its current owner from its permissions.
  2188. ;;; 
  2189. ;;; This shortcut will fail if someone has manually changed the workfile's
  2190. ;;; permissions; also if developers are munging the workfile in several
  2191. ;;; directories, with symlinks to a master (in this latter case, the
  2192. ;;; permissions shortcut will fail to detect a lock asserted from another
  2193. ;;; directory).
  2194. ;;; 
  2195. ;;; Note that these cases correspond exactly to the errors which could happen
  2196. ;;; because of a competing checkin/checkout race in between two instances of
  2197. ;;; vc-next-action.
  2198. ;;; 
  2199. ;;; For VC's purposes, a workfile/master pair may have the following states:
  2200. ;;; 
  2201. ;;; A. Unregistered.  There is a workfile, there is no master.
  2202. ;;; 
  2203. ;;; B. Registered and not locked by anyone.
  2204. ;;; 
  2205. ;;; C. Locked by calling user and unchanged.
  2206. ;;; 
  2207. ;;; D. Locked by the calling user and changed.
  2208. ;;; 
  2209. ;;; E. Locked by someone other than the calling user.
  2210. ;;; 
  2211. ;;; This makes for 25 states and 20 error conditions.  Here's the matrix:
  2212. ;;; 
  2213. ;;; VC's idea of state
  2214. ;;;  |
  2215. ;;;  V  Actual state   RCS action              SCCS action          Effect
  2216. ;;;    A  B  C  D  E
  2217. ;;;  A .  1  2  3  4   ci -u -t-          admin -fb -i<file>      initial admin
  2218. ;;;  B 5  .  6  7  8   co -l              get -e                  checkout
  2219. ;;;  C 9  10 .  11 12  co -u              unget; get              revert
  2220. ;;;  D 13 14 15 .  16  ci -u -m<comment>  delta -y<comment>; get  checkin
  2221. ;;;  E 17 18 19 20 .   rcs -u -M ; rcs -l unget -n ; get -g       steal lock
  2222. ;;; 
  2223. ;;; All commands take the master file name as a last argument (not shown).
  2224. ;;; 
  2225. ;;; In the discussion below, a "self-race" is a pathological situation in
  2226. ;;; which VC operations are being attempted simultaneously by two or more
  2227. ;;; Emacsen running under the same username.
  2228. ;;; 
  2229. ;;; The vc-next-action code has the following windows:
  2230. ;;; 
  2231. ;;; Window P:
  2232. ;;;    Between the check for existence of a master file and the call to
  2233. ;;; admin/checkin in vc-buffer-admin (apparent state A).  This window may
  2234. ;;; never close if the initial-comment feature is on.
  2235. ;;; 
  2236. ;;; Window Q:
  2237. ;;;    Between the call to vc-workfile-unchanged-p in and the immediately
  2238. ;;; following revert (apparent state C).
  2239. ;;; 
  2240. ;;; Window R:
  2241. ;;;    Between the call to vc-workfile-unchanged-p in and the following
  2242. ;;; checkin (apparent state D).  This window may never close.
  2243. ;;; 
  2244. ;;; Window S:
  2245. ;;;    Between the unlock and the immediately following checkout during a
  2246. ;;; revert operation (apparent state C).  Included in window Q.
  2247. ;;; 
  2248. ;;; Window T:
  2249. ;;;    Between vc-locking-user and the following checkout (apparent state B).
  2250. ;;; 
  2251. ;;; Window U:
  2252. ;;;    Between vc-locking-user and the following revert (apparent state C).
  2253. ;;; Includes windows Q and S.
  2254. ;;; 
  2255. ;;; Window V:
  2256. ;;;    Between vc-locking-user and the following checkin (apparent state
  2257. ;;; D).  This window may never be closed if the user fails to complete the
  2258. ;;; checkin message.  Includes window R.
  2259. ;;; 
  2260. ;;; Window W:
  2261. ;;;    Between vc-locking-user and the following steal-lock (apparent
  2262. ;;; state E).  This window may never close if the user fails to complete
  2263. ;;; the steal-lock message.  Includes window X.
  2264. ;;; 
  2265. ;;; Window X:
  2266. ;;;    Between the unlock and the immediately following re-lock during a
  2267. ;;; steal-lock operation (apparent state E).  This window may never cloce
  2268. ;;; if the user fails to complete the steal-lock message.
  2269. ;;; 
  2270. ;;; Errors:
  2271. ;;; 
  2272. ;;; Apparent state A ---
  2273. ;;;
  2274. ;;; 1. File looked unregistered but is actually registered and not locked.
  2275. ;;; 
  2276. ;;;    Potential cause: someone else's admin during window P, with
  2277. ;;; caller's admin happening before their checkout.
  2278. ;;; 
  2279. ;;;    RCS: ci will fail with a "no lock set by <user>" message.
  2280. ;;;    SCCS: admin will fail with error (ad19).
  2281. ;;; 
  2282. ;;;    We can let these errors be passed up to the user.
  2283. ;;; 
  2284. ;;; 2. File looked unregistered but is actually locked by caller, unchanged.
  2285. ;;; 
  2286. ;;;    Potential cause: self-race during window P.
  2287. ;;; 
  2288. ;;;    RCS: will revert the file to the last saved version and unlock it.
  2289. ;;;    SCCS: will fail with error (ad19).
  2290. ;;; 
  2291. ;;;    Either of these consequences is acceptable.
  2292. ;;; 
  2293. ;;; 3. File looked unregistered but is actually locked by caller, changed.
  2294. ;;; 
  2295. ;;;    Potential cause: self-race during window P.
  2296. ;;; 
  2297. ;;;    RCS: will register the caller's workfile as a delta with a
  2298. ;;; null change comment (the -t- switch will be ignored).
  2299. ;;;    SCCS: will fail with error (ad19).
  2300. ;;; 
  2301. ;;; 4. File looked unregistered but is locked by someone else.
  2302. ;;; 
  2303. ;;;    Potential cause: someone else's admin during window P, with
  2304. ;;; caller's admin happening *after* their checkout.
  2305. ;;; 
  2306. ;;;    RCS: will fail with a "no lock set by <user>" message.
  2307. ;;;    SCCS: will fail with error (ad19).
  2308. ;;; 
  2309. ;;;    We can let these errors be passed up to the user.
  2310. ;;; 
  2311. ;;; Apparent state B ---
  2312. ;;;
  2313. ;;; 5. File looked registered and not locked, but is actually unregistered.
  2314. ;;; 
  2315. ;;;    Potential cause: master file got nuked during window P.
  2316. ;;; 
  2317. ;;;    RCS: will fail with "RCS/<file>: No such file or directory"
  2318. ;;;    SCCS: will fail with error ut4.
  2319. ;;; 
  2320. ;;;    We can let these errors be passed up to the user.
  2321. ;;; 
  2322. ;;; 6. File looked registered and not locked, but is actually locked by the
  2323. ;;; calling user and unchanged.
  2324. ;;; 
  2325. ;;;    Potential cause: self-race during window T.
  2326. ;;; 
  2327. ;;;    RCS: in the same directory as the previous workfile, co -l will fail
  2328. ;;; with "co error: writable foo exists; checkout aborted".  In any other
  2329. ;;; directory, checkout will succeed.
  2330. ;;;    SCCS: will fail with ge17.
  2331. ;;; 
  2332. ;;;    Either of these consequences is acceptable.
  2333. ;;; 
  2334. ;;; 7. File looked registered and not locked, but is actually locked by the
  2335. ;;; calling user and changed.
  2336. ;;; 
  2337. ;;;    As case 6.
  2338. ;;; 
  2339. ;;; 8. File looked registered and not locked, but is actually locked by another
  2340. ;;; user.
  2341. ;;; 
  2342. ;;;    Potential cause: someone else checks it out during window T.
  2343. ;;; 
  2344. ;;;    RCS: co error: revision 1.3 already locked by <user>
  2345. ;;;    SCCS: fails with ge4 (in directory) or ut7 (outside it).
  2346. ;;; 
  2347. ;;;    We can let these errors be passed up to the user.
  2348. ;;; 
  2349. ;;; Apparent state C ---
  2350. ;;;
  2351. ;;; 9. File looks locked by calling user and unchanged, but is unregistered.
  2352. ;;; 
  2353. ;;;    As case 5.
  2354. ;;; 
  2355. ;;; 10. File looks locked by calling user and unchanged, but is actually not
  2356. ;;; locked.
  2357. ;;; 
  2358. ;;;    Potential cause: a self-race in window U, or by the revert's
  2359. ;;; landing during window X of some other user's steal-lock or window S
  2360. ;;; of another user's revert.
  2361. ;;; 
  2362. ;;;    RCS: succeeds, refreshing the file from the identical version in
  2363. ;;; the master.
  2364. ;;;    SCCS: fails with error ut4 (p file nonexistent).
  2365. ;;;
  2366. ;;;    Either of these consequences is acceptable.
  2367. ;;; 
  2368. ;;; 11. File is locked by calling user.  It looks unchanged, but is actually
  2369. ;;; changed.
  2370. ;;; 
  2371. ;;;    Potential cause: the file would have to be touched by a self-race
  2372. ;;; during window Q.
  2373. ;;; 
  2374. ;;;    The revert will succeed, removing whatever changes came with
  2375. ;;; the touch.  It is theoretically possible that work could be lost.
  2376. ;;; 
  2377. ;;; 12. File looks like it's locked by the calling user and unchanged, but
  2378. ;;; it's actually locked by someone else.
  2379. ;;; 
  2380. ;;;    Potential cause: a steal-lock in window V.
  2381. ;;; 
  2382. ;;;    RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
  2383. ;;;    SCCS: fails with error un2
  2384. ;;; 
  2385. ;;;    We can pass these errors up to the user.
  2386. ;;; 
  2387. ;;; Apparent state D ---
  2388. ;;;
  2389. ;;; 13. File looks like it's locked by the calling user and changed, but it's
  2390. ;;; actually unregistered.
  2391. ;;; 
  2392. ;;;    Potential cause: master file got nuked during window P.
  2393. ;;; 
  2394. ;;;    RCS: Checks in the user's version as an initial delta.
  2395. ;;;    SCCS: will fail with error ut4.
  2396. ;;;
  2397. ;;;    This case is kind of nasty.  It means VC may fail to detect the
  2398. ;;; loss of previous version information.
  2399. ;;; 
  2400. ;;; 14. File looks like it's locked by the calling user and changed, but it's
  2401. ;;; actually unlocked.
  2402. ;;; 
  2403. ;;;    Potential cause: self-race in window V, or the checkin happening
  2404. ;;; during the window X of someone else's steal-lock or window S of
  2405. ;;; someone else's revert.
  2406. ;;; 
  2407. ;;;    RCS: ci will fail with "no lock set by <user>".
  2408. ;;;    SCCS: delta will fail with error ut4.
  2409. ;;; 
  2410. ;;; 15. File looks like it's locked by the calling user and changed, but it's
  2411. ;;; actually locked by the calling user and unchanged.
  2412. ;;; 
  2413. ;;;    Potential cause: another self-race --- a whole checkin/checkout
  2414. ;;; sequence by the calling user would have to land in window R.
  2415. ;;; 
  2416. ;;;    SCCS: checks in a redundant delta and leaves the file unlocked as usual.
  2417. ;;;    RCS: reverts to the file state as of the second user's checkin, leaving
  2418. ;;; the file unlocked.
  2419. ;;;
  2420. ;;;    It is theoretically possible that work could be lost under RCS.
  2421. ;;; 
  2422. ;;; 16. File looks like it's locked by the calling user and changed, but it's
  2423. ;;; actually locked by a different user.
  2424. ;;; 
  2425. ;;;    RCS: ci error: no lock set by <user>
  2426. ;;;    SCCS: unget will fail with error un2
  2427. ;;; 
  2428. ;;;    We can pass these errors up to the user.
  2429. ;;; 
  2430. ;;; Apparent state E ---
  2431. ;;;
  2432. ;;; 17. File looks like it's locked by some other user, but it's actually
  2433. ;;; unregistered.
  2434. ;;; 
  2435. ;;;    As case 13.
  2436. ;;; 
  2437. ;;; 18. File looks like it's locked by some other user, but it's actually
  2438. ;;; unlocked.
  2439. ;;; 
  2440. ;;;    Potential cause: someone released a lock during window W.
  2441. ;;; 
  2442. ;;;    RCS: The calling user will get the lock on the file.
  2443. ;;;    SCCS: unget -n will fail with cm4.
  2444. ;;; 
  2445. ;;;    Either of these consequences will be OK.
  2446. ;;; 
  2447. ;;; 19. File looks like it's locked by some other user, but it's actually
  2448. ;;; locked by the calling user and unchanged.
  2449. ;;; 
  2450. ;;;    Potential cause: the other user relinquishing a lock followed by
  2451. ;;; a self-race, both in window W.
  2452. ;;; 
  2453. ;;;     Under both RCS and SCCS, both unlock and lock will succeed, making
  2454. ;;; the sequence a no-op.
  2455. ;;; 
  2456. ;;; 20. File looks like it's locked by some other user, but it's actually
  2457. ;;; locked by the calling user and changed.
  2458. ;;; 
  2459. ;;;     As case 19.
  2460. ;;; 
  2461. ;;; PROBLEM CASES:
  2462. ;;; 
  2463. ;;;    In order of decreasing severity:
  2464. ;;; 
  2465. ;;;    Cases 11 and 15 under RCS are the only one that potentially lose work.
  2466. ;;; They would require a self-race for this to happen.
  2467. ;;; 
  2468. ;;;    Case 13 in RCS loses information about previous deltas, retaining
  2469. ;;; only the information in the current workfile.  This can only happen
  2470. ;;; if the master file gets nuked in window P.
  2471. ;;; 
  2472. ;;;    Case 3 in RCS and case 15 under SCCS insert a redundant delta with
  2473. ;;; no change comment in the master.  This would require a self-race in
  2474. ;;; window P or R respectively.
  2475. ;;; 
  2476. ;;;    Cases 2, 10, 19 and 20 do extra work, but make no changes.
  2477. ;;; 
  2478. ;;;    Unfortunately, it appears to me that no recovery is possible in these
  2479. ;;; cases.  They don't yield error messages, so there's no way to tell that
  2480. ;;; a race condition has occurred.
  2481. ;;; 
  2482. ;;;    All other cases don't change either the workfile or the master, and
  2483. ;;; trigger command errors which the user will see.
  2484. ;;; 
  2485. ;;;    Thus, there is no explicit recovery code.
  2486.  
  2487. ;;; vc.el ends here
  2488.